Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement per-process wait() #116

Closed
giampaolo opened this issue May 23, 2014 · 14 comments
Closed

Implement per-process wait() #116

giampaolo opened this issue May 23, 2014 · 14 comments

Comments

@giampaolo
Copy link
Owner

From g.rodola on October 11, 2010 20:41:56

This would be the same as subprocess.Popen.wait() ( 
http://docs.python.org/library/subprocess.html#subprocess.Popen.wait 
) and it would be useful to wait for a process to terminate after a 
kill(), terminate() or send_signal() call in a "clean" way by also 
returning the process exit code.

subprocess module uses WaitForSingleObject() on Windows and 
os.waitpid() on Unix.
The latter one is already used in the test suite: 
https://code.google.com/p/psutil/source/browse/trunk/test/test_psutil.py?spec=svn671&r=666#75
 A further functionality I'd like to have is the possibility to set a timeout.
There's a patch on the python bug tracker which already do this that 
we can take as an example: http://bugs.python.org/issue5673

Original issue: http://code.google.com/p/psutil/issues/detail?id=116

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From g.rodola on October 11, 2010 13:28:08

It seems os.waitpid() can be used only for child processes (silly me, 
I should read the doc more carefully before writing code) while 
WaitForSingleObject() seems not to have this limitation, or at least 
this is the impression I got by reading the doc.
I'm not sure what to do. The only possibility I see aside from 
rejecting this proposal is:

- for child processes use os.waitpid()/WaitForSingleObject() and 
return their exit code

- for all other processes use is_running(), trap it in a while loop 
and return None when the process is terminated.

Anyway, for now this is the patch for POSIX systems.

Attachment: wait.patch

@giampaolo
Copy link
Owner Author

From g.rodola on October 11, 2010 13:31:51

Other possibility, which probably makes more sense: raise 
AccessDenied for non-child processes.

@giampaolo
Copy link
Owner Author

From [email protected] on October 13, 2010 13:40:12

In context of psutil, it probably makes more sense to use a polling 
method or something similar instead, so we can wait for any process to complete.

I can't think of a good use case for a wait() method off the top of 
my head, but I suspect most uses for a wait() would be either 1) 
launching a process and waiting for it to complete (in which case you 
would use subprocess instead, or 2) wanting to wait for some random 
system process to complete, in which case a method that only works on 
a child process won't be as useful.

@giampaolo
Copy link
Owner Author

From g.rodola on October 14, 2010 07:09:11

Yes, I agree. I wouldn't have proposed this in the first place if I 
knew this was possible only for children processes.
Closing out as WontFix.

Status: WontFix

@giampaolo
Copy link
Owner Author

From g.rodola on December 15, 2010 08:54:54

I'm reopening this, not because I think a wait() method is needed in 
a general sense, but because with the current terminate() and kill() 
methods implementation we have no guarantee that the process has 
disappeared and this is not good.

This appears clear if we take a look at the test suite and search for "wait()".
In many circumstances (8, to be precise) we kill/terminate a process 
for testing purposes and then explicitly use subprocess wait() method 
to make sure that we can go on only when the process is gone.

My proposal is to make terminate()/kill() return only when the 
process has actually disappeared and provide a "timeout" argument 
similar to socket.create_connection:

- None == no timeout, return only when the process is gone
- 0 == return/raise immediately
- > 0 == return/raise after a while

psutil should also grow a TimeoutExpired exception.

Status: ReOpened

@giampaolo
Copy link
Owner Author

From [email protected] on December 15, 2010 09:44:43

If we make the default for the timeout to just return immediately the 
same as it works now, then I'm ok with this. 

I don't think it's a logical behavior for a kill() command to wait on 
a process disappearing completely so I'd favor making that something 
you have to explicitly enable with a parameter.

@giampaolo
Copy link
Owner Author

From g.rodola on December 15, 2010 09:57:40

Thinking about command line "kill" command I agree with you, by 
default it should return immediately.

> I'd favor making that something you have to explicitly enable with a parameter.

In this case a parameter called "wait" would be more appropriate.

Status: Accepted
Labels: Milestone-0.2.1

@giampaolo
Copy link
Owner Author

From g.rodola on December 24, 2010 07:19:56

I decided to provide a generic Process.wait() method which I 
implemented for all UNIX systems in r850 .
For processes which are children of the current one just return None 
instead of the exit code.

Labels: -Progress-0in4 Progress-3in4

@giampaolo
Copy link
Owner Author

From g.rodola on December 24, 2010 08:18:34

Implemented for Windows in r852 .

Status: FixedInSVN
Labels: -Progress-3in4 Progress-4in4

@giampaolo
Copy link
Owner Author

From g.rodola on February 17, 2011 16:20:01

In r924 I added a "timeout" parameter for wait() and defined a new 
TimeoutExpired exception.
This is useful for both Process and Popen classes
Popen.wait() method is automatically overridden and provides this new feature natively.
Still have to take care of Windows.

@giampaolo
Copy link
Owner Author

From g.rodola on February 17, 2011 16:58:06

Implemented on Windows in r925 .

@giampaolo
Copy link
Owner Author

From g.rodola on March 05, 2011 04:36:53

Just for keeping it somewhere or in case it will be useful in future 
here is an alternative implementation using signals.

Attachment: waitpid_with_signal.py

@giampaolo
Copy link
Owner Author

From g.rodola on March 20, 2011 14:55:37

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:55:18

Updated csets after the SVN -> Mercurial migration: r850 == revision 
e5beea400d1e r852 == revision dc0c8f51b45e r924 == revision 
200f6c59086d r925 == revision 6673a9ab4478

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant