Issue 6554: Do we have something like os.pid_exists()? (original) (raw)
Issue6554
Created on 2009-07-23 18:28 by pmolina, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (9) | ||
---|---|---|
msg90850 - (view) | Author: Patricio Mariano Molina (pmolina) | Date: 2009-07-23 18:28 |
I couldn't find anything like os.pid_exists() in Python 2.5/2.6, neither in bugs.python.org (this *could* be a dupe) Do we have something like that? Right now I'm doing this: try: os.kill(int(pid), 0) return True except OSError: return False I'd love to do the same without catching an exception (they're expensive!), maybe in C? Thanks! | ||
msg90851 - (view) | Author: Jean-Paul Calderone (exarkun) * ![]() |
Date: 2009-07-23 18:31 |
This is quite a microoptimization. Why do you think you need to avoid the exception here? | ||
msg90852 - (view) | Author: Jean-Paul Calderone (exarkun) * ![]() |
Date: 2009-07-23 18:35 |
For what it's worth, here are some timings from my system. First, os.kill without raising an exception: exarkun@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@boson:~$ Next, os.kill without raising an exception, but with exception handling: exarkun@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@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. | ||
msg90854 - (view) | Author: Patricio Mariano Molina (pmolina) | Date: 2009-07-23 18:47 |
Hey Jean-Paul, thanks for the quick reply! You're right, but I wasn't thinking too much about optimization: I think it would be useful to have that simple function, returning True or False. I use to search for active PIDs *a lot* with Python Do you think is a "YAGNI" case? | ||
msg90855 - (view) | Author: Jean-Paul Calderone (exarkun) * ![]() |
Date: 2009-07-23 18:50 |
It might be better to pick a better (but probably platform-specific) API for such a use-case. os.kill has a problem with false positives (on Linux it will tell you a process exists even when it doesn't). Looking in /proc/ or using a Windows API to enumerate all existing processes might be better. These have the advantage of giving you a bunch of PID information all at once which you can then check against, rather than repeatedly making syscalls. | ||
msg90856 - (view) | Author: Patricio Mariano Molina (pmolina) | Date: 2009-07-23 18:56 |
Sounds good. And what do you think about os.pid_exists() using /proc/ or a Windows API? | ||
msg90859 - (view) | Author: Jean-Paul Calderone (exarkun) * ![]() |
Date: 2009-07-23 19:14 |
The os module is mostly for wrappers around native platform APIs. So at the very least, I don't think such an API belongs there. It would fit in to a general process-related module, perhaps. | ||
msg90867 - (view) | Author: Martin v. Löwis (loewis) * ![]() |
Date: 2009-07-23 23:28 |
> I'd love to do the same without catching an exception (they're > expensive!) Why do you say that? I don't believe that exceptions are expensive (e.g. compared to the system call) | ||
msg109676 - (view) | Author: Terry J. Reedy (terry.reedy) * ![]() |
Date: 2010-07-09 04:17 |
Avoiding an exception for a very rare use case is an inadequate reason to add a new function. Requests to add someone's favorite short, specialized, and easy-to-write function are a common feature of python-list. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:51 | admin | set | github: 50803 |
2010-07-09 04:17:49 | terry.reedy | set | status: open -> closedversions: + Python 3.2, - Python 2.6, Python 2.5nosy: + terry.reedymessages: + resolution: rejected |
2009-07-23 23:28:53 | loewis | set | nosy: + loewistitle: Do we have something like os.pid_exists()? -> Do we have something like os.pid_exists()?messages: + |
2009-07-23 19:14:01 | exarkun | set | messages: + |
2009-07-23 18:56:24 | pmolina | set | messages: + |
2009-07-23 18:50:21 | exarkun | set | messages: + |
2009-07-23 18:47:10 | pmolina | set | messages: + |
2009-07-23 18:35:00 | exarkun | set | messages: + |
2009-07-23 18:31:14 | exarkun | set | nosy: + exarkunmessages: + |
2009-07-23 18:28:34 | pmolina | create |