开发者

implementing a timer in a django app

开发者 https://www.devze.com 2022-12-17 11:54 出处:网络
In my Django app, I need to implement this \"timer-based\" functionality: User creates some jobs and for each one defines when (in the same unit the timer works, probably seconds) it开发者_Python百

In my Django app, I need to implement this "timer-based" functionality:

  1. User creates some jobs and for each one defines when (in the same unit the timer works, probably seconds) it开发者_Python百科 will take place.
  2. User starts the timer.
  3. User may pause and resume the timer whenever he wants.
  4. A job is executed when its time is due.

This does not fit a typical cron scenario as time of execution is tied to a timer that the user can start, pause and resume.

What is the preferred way of doing this?


This isn't a Django question. It is a system architecture problem. The http is stateless, so there is no notion of times.

My suggestion is to use Message Queues such as RabbitMQ and use Carrot to interface with it. You can put the jobs on the queue, then create a seperate consumer daemon which will process jobs from the queue. The consumer has the logic about when to process.

If that it too complex a system, perhaps look at implementing the timer in JS and having it call a url mapped to a view that processes a unit of work. The JS would be the timer.


Have a look at Pinax, especially the notifications.

Once created they are pushed to the DB (queue), and processed by the cron-jobbed email-sending (2. consumer).

In this senario you won't stop it once it get fired. That could be managed by som (ajax-)views, that call system process....

edit

instead of cron-jobs you could use a twisted-based consumer:

  • write jobs to db with time-information to the db

  • send a request for consuming (or resuming, pausing, ...) to the twisted server via socket

  • do the rest in twisted


You're going to end up with separate (from the web server) processes to monitor the queue and execute jobs. Consider how you would build that without Django using command-line tools to drive it. Use Django models to access the the database.

When you have that working, layer on on a web-based interface (using full Django) to manipulate the queue and report on job status.

I think that if you approach it this way the problem becomes much easier.


I used the probably simplest (crudest is more appropriate, I'm afraid) approach possible: 1. Wrote a model featuring the current position and the state of the counter (active, paused, etc), 2. A django job that increments the counter if its state is active, 3. An entry to the cron that executes the job every minute.

Thanks everyone for the answers.


You can always use a client based jquery timer, but remember to initialize the timer with a value which is passed from your backend application, also make sure that the end user didn't edit the time (edit by inspecting). So place a timer start time (initial value of the timer) and timer end time or timer pause time in the backend (DB itself).

Monitor the duration in the backend and trigger the job ( in you case ).

Hope this is clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消