nikademus79 / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Make Process.wait() busy loop on UNIX a little clever #245

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Process.wait(timeout=N) on UNIX is implemented as a busy loop which keeps 
calling os.waitpid() until the process is gone or the timeout is reached.
A time.sleep() call is used to avoid hogging CPU resources:
http://code.google.com/p/psutil/source/browse/tags/release-0.4.1/psutil/_psposix
.py#53
This is not very smart though, as it keeps sleeping for 0.0001 seconds.
I noticed a better solution used in Lib/subprocess.py which increases the sleep 
interval incrementally, saving a considerable amount of CPU cycles if the 
process dies, say, after 20 seconds:
http://hg.python.org/cpython/file/053fbceaf789/Lib/subprocess.py#l1407
I think it's worth to take that as an example.
Also, we might want to keep an eye on http://bugs.python.org/issue12187 and 
hopefully get rid of the busy loop on some UNIX platforms.

Original issue reported on code.google.com by g.rodola on 19 Jan 2012 at 8:35

GoogleCodeExporter commented 8 years ago
Fixed in r1252.

Original comment by g.rodola on 20 Jan 2012 at 6:53

GoogleCodeExporter commented 8 years ago
I didn't get a chance to review this one; did you put in some kind of safeguard 
upper limit? My only concern would be making the sleep delay too large could 
have a negative impact on functionality.

Original comment by jlo...@gmail.com on 20 Jan 2012 at 8:16

GoogleCodeExporter commented 8 years ago
Yep, the limit is 0.04 seconds.

Original comment by g.rodola on 20 Jan 2012 at 8:19

GoogleCodeExporter commented 8 years ago
Excellent, nice find on this one

Original comment by jlo...@gmail.com on 20 Jan 2012 at 8:30

GoogleCodeExporter commented 8 years ago

Original comment by g.rodola on 30 Jan 2012 at 12:19

GoogleCodeExporter commented 8 years ago
0.5.0 is finally out. Closing out as fixed.

Original comment by g.rodola on 27 Jun 2012 at 6:54

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Updated csets after the SVN -> Mercurial migration:
r1252 == revision 18d53ff30011

Original comment by g.rodola on 2 Mar 2013 at 12:06