import asyncio from functools import partial from subprocess import run semaphore = asyncio.Semaphore(2) def generate_cmd(opts): return "cmd" async def process_pdf(ctx, file_path, opts): async with semaphore: # Set status to running cmd = generate_cmd(opts) process = await asyncio.create_subprocess_exec(cmd) await process.await() # Set status to done.