[issue6554] Do we have something like os.pid_exists()?

Jean-Paul Calderone report at bugs.python.org
Thu Jul 23 20:35:02 CEST 2009


Jean-Paul Calderone <exarkun at divmod.com> added the comment:

For what it's worth, here are some timings from my system.  First,
os.kill without raising an exception:

exarkun at boson:~$ python -m timeit -s 'import os; pid = os.getpid()' '
os.kill(pid, 0)
'
1000000 loops, best of 3: 0.413 usec per loop
exarkun at boson:~$ 

Next, os.kill without raising an exception, but with exception handling:

exarkun at boson:~$ python -m timeit -s 'import os; pid = os.getpid()' '
> try:
>     os.kill(pid, 0)
> except OSError, e:
>     pass
> '
1000000 loops, best of 3: 0.42 usec per loop

Finally, os.kill with exception handling and raising an exception:

exarkun at boson:~$ python -m timeit -s 'import os; pid = os.getpid()' '
try:
    os.kill(pid + 1, 0)
except OSError, e:
    pass
'
100000 loops, best of 3: 2.58 usec per loop

The slowest case is almost 7x slower than the fastest case.  However,
this is only triggered when os.kill raises an exception (ie, if the pid
does exist, it's still fast).  Plus, this "slow" case still only takes
two and a half microseconds.  That's pretty fast.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6554>
_______________________________________


More information about the Python-bugs-list mailing list