By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,547 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,547 IT Pros & Developers. It's quick & easy.

Processes with timeout

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.