I’m building a Telegram bot where users can send videos for editing. The bot downloads the videos, applies edits, and sends them back to the users.
I encountered an issue with async processing. When the bot runs in a synchronous mode, it blocks the entire operation until the video is edited, preventing other users from interacting with the bot. However, when I switch to asynchronous processing, the bot attempts to send the edited video even before the download is complete.
Here’s a snippet of my code:
handle_video(user_input, f"{download_folder}/{str(user_id)}/{profile}/{media_hash}.mp4", media_hash)
video_path = f"./exported/{str(user_id)}/{profile}/{media_hash}.mp4"
await bot.send_video(chat_id=user_id, video=open(video_path, 'rb'))
And this is my video handling function:
async def handle_video(title, the_hash, user_id):
subprocess.run(["python", "./video_editor.py", profile, title, the_hash, str(user_id)])
I have tried different ways to structure this but nothing seems to work. How can I avoid this blockage while ensuring the subprocess executes correctly?
yeah, subprocess.run() is blocking ur async. u should use asyncio.create_subprocess_exec() instead. also, make sure to await handle_video() so it waits for processing b4 sending the video back. that should help with the async issue!
You’re mixing synchronous subprocess calls with async code. subprocess.run() blocks the event loop even inside an async function. I hit the same issue building a document processing bot. Switch to asyncio.create_subprocess_exec() and actually await the subprocess completion. Your code doesn’t await the handle_video() call - it fires off the subprocess then immediately tries sending the video. Here’s what worked for me: await handle_video(user_input, f"{download_folder}/{str(user_id)}/{profile}/{media_hash}.mp4", media_hash) video_path = f"./exported/{str(user_id)}/{profile}/{media_hash}.mp4" await bot.send_video(chat_id=user_id, video=open(video_path, ‘rb’)) Then modify your handle_video function to wait for subprocess completion: async def handle_video(title, the_hash, user_id): process = await asyncio.create_subprocess_exec(“python”, “./video_editor.py”, profile, title, the_hash, str(user_id)) await process.wait() This keeps your event loop free while videos process in the background.
Had the same issue building a file processing bot. Your main thread keeps running after calling handle_video because you’re not waiting for it to finish. You need to await the handle_video call. Try using asyncio.run_in_executor() instead of create_subprocess_exec if you want to stick with subprocess.run(): loop = asyncio.get_event_loop() await loop.run_in_executor(None, subprocess.run, [“python”, “./video_editor.py”, profile, title, the_hash, str(user_id)]) This keeps your existing subprocess code but makes it non-blocking. The executor runs the blocking call in a separate thread pool so your main event loop stays free for other users. Add error handling around the subprocess call in case video editing fails.