Processes with timeout

Discussion in 'Python' started by Markus Franz, Apr 19, 2004.

  1. Markus Franz

    Markus Franz Guest

    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
    Markus Franz, Apr 19, 2004
    #1
    1. Advertising

  2. Markus Franz

    Ivan Voras Guest

    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)
    Ivan Voras, Apr 19, 2004
    #2
    1. Advertising

  3. 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
    Mark Borgerding, Apr 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,735
    Bob Johnson
    Aug 7, 2003
  2. =?Utf-8?B?amVzdGVy?=

    Background processes and session timeout

    =?Utf-8?B?amVzdGVy?=, Apr 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    536
    =?Utf-8?B?amVzdGVy?=
    Apr 29, 2005
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,083
    David Schwartz
    Dec 9, 2003
  4. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,267
    Brian Candler
    Oct 6, 2004
  5. Marc Heiler
    Replies:
    1
    Views:
    160
    Robert Klemme
    May 24, 2009
Loading...

Share This Page