469,333 Members | 4,354 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,333 developers. It's quick & easy.

Processes with timeout

Hi.

My little Python script creates some child-processes depending on how much
command line options were given:

for myvalue in sys.argv[1:]:

pid = os.fork()

if pid == 0:
do_something() # placeholder for operations

break

Now I do some difficult things inside each child process (above: function
do_something). These operations may take a long time.

How can I make it sure that every child-process terminates after x senconds
(wether it has finished or not)?
I don't know how to force a child-process to kill itself after x seconds. I
also don't know how to force a main process (parent) to kill a child-process
after x seconds.

I searched for something like:

for myvalue in sys.argv[1:]:

pid = os.fork()

if pid == 0:
os.exit(timeout)

do_something() # placeholder for operations

break

But there was nothing like this... Do you have an answer to my question???
Thank you.

Best regards

Markus Franz
Jul 18 '05 #1
2 1554
Markus Franz wrote:
How can I make it sure that every child-process terminates after x senconds
(wether it has finished or not)?


You can toy around with signals. From outside the child process, you can
send it SIGTERM after some time has passed (and catch it in the process).
From inside the process, you can use SIGALARM to track when the time has
expired. Or you can combine the two.

Or, you can start a "watchdog thread" (a thread that mostly sleep()-s, but
now and then checks the time)

Jul 18 '05 #2
Markus Franz wrote:
How can I make it sure that every child-process terminates after x senconds
(wether it has finished or not)?
I don't know how to force a child-process to kill itself after x seconds. I
also don't know how to force a main process (parent) to kill a child-process
after x seconds.


signal.alarm(x)

# after x seconds, a SIGALRM signal will be sent to the current process
# (even if the contents of the process changes via exec* )

-- Mark Borgerding

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by yawnmoth | last post: by
2 posts views Thread by Markus Franz | last post: by
4 posts views Thread by DavidS | last post: by
2 posts views Thread by jester | last post: by
1 post views Thread by CSN | last post: by
2 posts views Thread by schreckmail | last post: by
3 posts views Thread by Anbu | last post: by
22 posts views Thread by Nick Craig-Wood | last post: by
3 posts views Thread by pbd22 | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.