Summary
- OS: all
- Type: new-api
Description
I'm using psutil in an asyncio-based scheduler and would benefit from async versions of some Process methods. Currently, I have to wrap blocking calls like process.wait() using asyncio.to_thread() or run them in a background thread, which adds overhead and complexity.
Requested async methods
Process.async_wait()- Asynchronously wait for process termination. CurrentlyProcess.wait()blocks the calling thread. An async version would allow efficient waiting in asyncio event loops without consuming a thread.Process.async_is_running()- Asynchronously check if process is running. Whileis_running()is typically fast, an async version would allow it to be used consistently in async contexts without blocking the event loop.
Current workaround
import asyncio import psutil async def wait_for_process(pid: int) -> int: process = psutil.Process(pid) return await asyncio.to_thread(process.wait)
This works but requires a thread from the thread pool for each waiting process.
Proposed API
import psutil async def example(): process = psutil.Process(pid) # Wait for process to finish exit_code = await process.async_wait() # Check if running (for consistency in async code) is_running = await process.async_is_running()
Would you consider inclusion of such async methods in psutil (I can submit a PR)? Or is it out of scope?