Vishal
Vishal

Reputation: 20617

A daemon to call a function every 2 minutes with start and stop capablities

I am working on a django web application.

A function 'xyx' (it updates a variable) needs to be called every 2 minutes.

I want one http request should start the daemon and keep calling xyz (every 2 minutes) until I send another http request to stop it.

Appreciate your ideas.

Thanks Vishal Rana

Upvotes: 1

Views: 659

Answers (3)

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340211

If the easy solutions (loop in a script, crontab signaled by a temp file) are too fragile for your intended usage, you could use Twisted facilities for process handling and scheduling and networking. Your Django app (using a Twisted client) would simply communicate via TCP (locally) with the Twisted server.

Upvotes: 2

jamesbtate
jamesbtate

Reputation: 1399

Probably a little hacked but you could try this:

Set up a crontab entry that runs a script every two minutes. This script will check for some sort of flag (file existence, contents of a file, etc.) on the disk to decide whether to run a given python module. The problem with this is it could take up to 1:59 to run the function the first time after it is started.

I think if you started a daemon in the view function it would keep the httpd worker process alive as well as the connection unless you figure out how to send a connection close without terminating the django view function. This could be very bad if you want to be able to do this in parallel for different users. Also to kill the function this way, you would have to somehow know which python and/or httpd process you want to kill later so you don't kill all of them.

The real way to do it would be to code an actual daemon in w/e language and just make a system call to "/etc/init.d/daemon_name start" and "... stop" in the django views. For this, you need to make sure your web server user has permission to execute the daemon.

Upvotes: 2

user257111
user257111

Reputation:

There are a number of ways to achieve this. Assuming the correct server resources I would write a python script that calls function xyz "outside" of your django directory (although importing the necessary stuff) that only runs if /var/run/django-stuff/my-daemon.run exists. Get cron to run this every two minutes.

Then, for your django functions, your start function creates the above mentioned file if it doesn't already exist and the stop function destroys it.

As I say, there are other ways to achieve this. You could have a python script on loop waiting for approx 2 minutes... etc. In either case, you're up against the fact that two python scripts run on two different invocations of cpython (no idea if this is the case with mod_wsgi) cannot communicate with each other and as such IPC between python scripts is not simple, so you need to use some sort of formal IPC (like semaphores, files etc) rather than just common variables (which won't work).

Upvotes: 4

Related Questions