views:

280

answers:

5

I need to run a server side script like Python "forever" (or as long as possible without loosing state), so they can keep sockets open and asynchronously react to events like data received. For example if I use Twisted for socket communication.

  • How would I manage something like this?
  • Am I confused? or are there are better ways to implement asynchronous socket communication?
  • After starting the script once via Apache server, how do I stop it running?
+1  A: 

You can just write an script that is continuously in a while block waiting for the connection to happen and waits for a signal to close it.

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

Then to stop it you just need to run another script that sends that signal to him.

Rafa de Castro
A: 

I think Comet is what you're looking for. Make sure to take a look at Tornado too.

Gonzalo
+1  A: 

You can use a ‘double fork’ to run your code in a new background process unbound to the old one. See eg this recipe with more explanatory comments than you could possibly want.

I wouldn't recommend this as a primary way of running background tasks for a web site. If your Python is embedded in an Apache process, for example, you'll be forking more than you want. Better to invoke the daemon separately (just under a similar low-privilege user).

After starting the script once via Apache server, how do I stop it running?

You have your second fork write the process number (pid) of the daemon process to a file, and then read the pid from that file and send it a terminate signal (os.kill(pid, signal.SIG_TERM)).

Am I confused?

That's the question! I'm assuming you are trying to have a background process that responds on a different port to the web interface for some sort of unusual net service. If you merely talking about responding to normal web requests you shoudn't be doing this, you should rely on Apache to handle your sockets and service one request at a time.

bobince
A: 

You may want to look at FastCGI, it sounds exactly like what you are looking for, but I'm not sure if it's under current development. It uses a CGI daemon and a special apache module to communicate with it. Since the daemon is long running, you don't have the fork/exec cost. But as a cost of managing your own resources (no automagic cleanup on every request)

One reason why this style of FastCGI isn't used much anymore is there are ways to embed interpreters into the Apache binary and have them run in server. I'm not familiar with mod_python, but i know mod_perl has configuration to allow long running processes. Be careful here, since a long running process in the server can cause resource leaks.

A general question is: what do you want to do? Why do you need this second process, but yet somehow controlled by apache? Why can'ty ou just build a daemon that talks to apache, why does it have to be controlled by apache?

Rich Homolka
+3  A: 

If you are using twisted then it has a whole infrastructure for starting and stopping daemons.

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

How would I manage something like this?

Twisted works well for this, read the link above

Am I confused? or are there are better ways to implement asynchronous socket communication?

Twisted is very good at asynchronous socket communications. It is hard on the brain until you get the hang of it though!

After starting the script once via Apache server, how do I stop it running?

The twisted tools assume command line access, so you'd have to write a cgi wrapper for starting / stopping them if I understand what you want to do.

Nick Craig-Wood