Asyncio safe methods

24 views
Skip to first unread message

Amir Rossert

unread,
Jun 5, 2023, 3:22:39 AM6/5/23
to psutil
I'm using psutil in an asyncio application and wondering what methods are "asyncio" safe that will not block the event loop and what is blocking that needs to be executed in an executor? 

Does calling the constructor safe not block the event loop "psutil.Process()"?
What is the recommended way to use psutil to extract some information from the process like name / cmdline / status ... without blocking the event loop?

Giampaolo Rodola'

unread,
Jun 5, 2023, 3:49:59 AM6/5/23
to psu...@googlegroups.com
Hi, 
interesting question. I would say that in general the majority of psutil APIs are fast enough and it's OK to call them directly without blocking, meaning you won't have to use asyncio's run_in_executor().
A small set of psutil APIs are more resource intensive than others though. In the github repo there is a script you can use to measure the time of each API call and get a rough idea:
E.g. on my machine (Linux, Intel i7 2.60 GhZ) the slowest process API is Process.memory_maps(), which takes ~0.002 secs per call. I'm not sure if that can be considered "blocking" for asyncio. I guess it depends on the workload, how often you call the "slow API" in a second, how many concurrent clients you're serving, etc.
As a general rule I would say:

1) get a list of all psutil APIs you use in your app
3) figure out if you have "slow APIs" and run those ones (and only those) via asyncio run_in_executor(), else call them directly 

Alternatively, what you can do is:

1) get all psutil info in one shot (multiple API calls)
2) do that using asyncio run_in_executor()

Hope this helps.


--
You received this message because you are subscribed to the "Python process utilities (psutil)" project group:
https://groups.google.com/g/psutil
To post to this group, send email to psu...@googlegroups.com
To unsubscribe from this group, send email to psutil-un...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "psutil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psutil+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psutil/631d7da9-3840-435b-ac62-3eeed9f22673n%40googlegroups.com.


--
Giampaolo - gmpy.dev

Reply all
Reply to author
Forward
0 new messages