Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Processes with timeout

Reply
Thread Tools

Processes with timeout

 
 
Markus Franz
Guest
Posts: n/a
 
      04-19-2004
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


 
Reply With Quote
 
 
 
 
Ivan Voras
Guest
Posts: n/a
 
      04-19-2004
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)

 
Reply With Quote
 
 
 
 
Mark Borgerding
Guest
Posts: n/a
 
      04-20-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling processes and what to "feed" other processes Marc Heiler Ruby 1 05-24-2009 05:37 PM
Background processes and session timeout =?Utf-8?B?amVzdGVy?= ASP .Net 2 04-29-2005 12:06 AM
Timeout::timeout and Socket timeout Mark Probert Ruby 1 10-06-2004 09:30 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
Re: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Bob Johnson ASP .Net 0 08-07-2003 12:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57