I have to operate a server side script like Python "forever" (or as lengthy as you possibly can without losing condition), to allow them to keep sockets open and asynchronously respond to occasions like data received. For instance basically use Twisted for socket communication.

  • Wouldso would I manage something similar to this?
  • Shall We Be Held confused? or are you will find possible ways to implement asynchronous socket communication?
  • After beginning the script once via Apache server, how do you stop it running?

If you work with twisted then it features a whole infrastructure for beginning and preventing daemons.

http://twistedmatrix.com/projects/core/documentation/howto/application.html

Wouldso would I manage something similar to this?

Twisted can be useful for this, browse the link above

Shall We Be Held confused? or are you will find possible ways to implement asynchronous socket communication?

Twisted is excellent at asynchronous socket communications. It's difficult on the mind before you get used to it though!

After beginning the script once via Apache server, how do you stop it running?

The twisted tools assume command line access, so you'd need to write a cgi wrapper for beginning / preventing them basically know very well what for you to do.

You can easily write an script that's continuously shortly block awaiting the bond to occur and waits for any signal to shut it.

http://docs.python.org/library/signal.html

Then to prevent it you need to simply run another script that transmits that signal to him.

Use a ‘double fork’ to operate your code inside a new background process unbound towards the old one. See eg this recipe with increased explanatory comments than you may want.

I would not recommend this like a primary method of running background tasks for an internet site. In case your Python is baked into an Apache process, for instance, you will be forking a lot more than you would like. Easier to invoke the daemon individually (just below an identical low-privilege user).

After beginning the script once via Apache server, how do you stop it running?

You've your next fork write the procedure number (pid) from the daemon process to some file, after which browse the pid from that file and send it a terminate signal (os.kill(pid, signal.SIG_TERM)).

Shall We Be Held confused?

This is the question! I am presuming you are attempting to possess a background procedure that responds on the different port to the net interface for many kind of unusual internet service. Should you basically speaking about reacting to normalcy web demands you shoudn't do this, you need to depend on Apache to deal with your electrical sockets and repair one request at any given time.

I believe Comet is exactly what you are searching for. Make certain to have a look at Tornado too.

You might want to take a look at FastCGI, it may sound the same as what you're searching for, but I am unsure whether it's under current development. It utilizes a CGI daemon along with a special apache module to talk with it. Because the daemon is lengthy running, you do not have the fork/professional cost. But like a price of controlling your personal assets (no automagic cleanup on every request)

One good reason why this kind of FastCGI is not used much any longer is you will find methods to embed interpreters in to the Apache binary and also have them run in server. I am unfamiliar with mod_python, however i know mod_perl has configuration to permit lengthy running processes. Be cautious here, since a lengthy running process within the server may cause resource leaks.

An over-all real question is: give me an idea to complete? Why do you want this second process, and yet in some way controlled by apache? Why can'ty ou just develop a daemon that foretells apache, why does it need to be controlled by apache?