Processes with timeout

M

Markus Franz

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
 
I

Ivan Voras

Markus said:
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)
 
M

Mark Borgerding

Markus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top