1

I have an issue where I am trying to have a thread in Python sleep in the background for a specified time. This needs to be done in the program though, not at the command-line level, as there is data being passed out of the Python program into PHP.

import os
from threading import Thread

class getPID(Thread):
    def run(self):
        print os.getpid()

class waitToKill(Thread):
    def run(self):
        time.sleep(25) #This is the sleep that needs to be done in the background
        os.system('pkill process')

getPID().start()
waitToKill().start()

The concept is that PHP will repeatedly start the Python program, then take the PID and kill the process before it hits the pkill statement. When someone leaves the page, the Python program will have been started, but not killed, thusly reaching the pkikll statement and closing the process started by entering the page.

This is about as simple as I can conceive making a Dead Man's Switch in PHP/Python, but it requires background sleep, as the PHP exec() function does not run things in the background well while still receiving std output. (i.e. exec(dms.py &) will hang for 25s before returning the PID and displaying the rest of the page)

What is the best way to accomplish this? Should it be at the time.sleep() level or the thread level? Or can the PHP exec() function be modified to send output while still running in the background?

Thanks for any help you can provide!

MasonWinsauer
  • 673
  • 3
  • 14
  • 32
  • It sounds like you want the process to fork in the background. In that case, you may need to use [os.fork()](http://docs.python.org/library/os#os.fork). The problem with that is that the process ID will be different for the forked process, so you'll need a way to extract that and print it to the screen. – 101100 Aug 06 '12 at 18:16
  • 1
    You may want to look at `threading.Timer` objects. You give it a time and a function. after calling `.start()` the Timer will wait however long you specified, then call the function provided. It seems like you're kind of making your own version of that here http://docs.python.org/library/threading.html#timer-objects – Ryan Haining Aug 06 '12 at 18:49

1 Answers1

0

It is totally unnecessary to use threading for this task--the process will not be considered finished until all threads have exited, so there is no benefit to having the main thread finish early.

You will have to do one of:

  • Execute the script from PHP in such a way that it does not have to wait for the process to finish (see: php execute a background process)
  • Fork the python process or in some other way start a new python process such that the original can exit quickly
Community
  • 1
  • 1
Luke
  • 11,374
  • 2
  • 48
  • 61
  • a) Your first point is one I addressed as having issues with b) This is a stripped down version of a program in which threading is very necessary, but not part of the issue c) I will do research into how forking would work in this situation Thanks – MasonWinsauer Aug 06 '12 at 18:46
  • I don't remember much PHP, but I'll bet there is an asynchronous alternative to exec(). Hopefully someone else can chime in.. – Luke Aug 06 '12 at 18:48
  • Something like this: http://stackoverflow.com/questions/45953/php-execute-a-background-process#45966 – Luke Aug 06 '12 at 18:52
  • Yeah, I was trying very hard to avoid writing the output to a file, but that would be acceptable, I suppose =) Thanks! – MasonWinsauer Aug 06 '12 at 19:16