Python subprocess: callback when cmd exits

Posted by Anon on Stack Overflow See other posts from Stack Overflow or by Anon
Published on 2010-04-05T23:45:06Z Indexed on 2010/04/05 23:53 UTC
Read the original article Hit count: 322

Filed under:
|
|
|

Hi,

I'm currently launching a programme using subprocess.Popen(cmd, shell=TRUE)

I'm fairly new to Python, but it 'feels' like there ought to be some api that lets me do something similar to:

subprocess.Popen(cmd, shell=TRUE,  postexec_fn=function_to_call_on_exit)

I am doing this so that function_to_call_on_exit can do something based on knowing that the cmd has exited (for example keeping count of the number of external processes currently running)

I assume that I could fairly trivially wrap subprocess in a class that combined threading with the Popen.wait() method, but as I've not done threading in Python yet and it seems like this might be common enough for an API to exist, I thought I'd try and find one first.

Thanks in advance :)

© Stack Overflow or respective owner

Related posts about python

Related posts about subprocess