Processes with timeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Franz

    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


  • Ivan Voras

    #2
    Re: Processes with timeout

    Markus Franz wrote:[color=blue]
    > How can I make it sure that every child-process terminates after x senconds
    > (wether it has finished or not)?[/color]

    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)

    Comment

    • Mark Borgerding

      #3
      Re: Processes with timeout

      Markus Franz wrote:[color=blue]
      > 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.[/color]

      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

      Comment

      Working...