saeedgnu
saeedgnu

Reputation: 4366

Python, Django and event loop (periodic jobs)

I am developing a Python applicaton for server that uses Django + WSGI + Apache under Debian Linux. The application has web interface as well as command line interface (that still uses django models..., just does not use views and templates).
Database backend is SQLite3.

This applications also needs to run some jobs periodically. I wrote a unix-like daemon that uses python-gobject and python-glib, and runs those jobs like this:

gobject.timeout_add_seconds(seconds, someCallback...)
gobject.timeout_add_seconds(seconds, someCallback...)
...
gobject.timeout_add_seconds(seconds, someCallback...)

glib.MainLoop().run()

I tested it, and there are some strange problems on written data in sqlite db. I think that's because there are two Python instances reading and writing from/to a single sqlite db. One for apache+wsgi and one for my own daemon. (Or event 3 Python instances, when I use command line interface)

My question is, what do recommend me to do? Put those timeout_add and MainLoop in my "dj_survey.wsgi" to run on apache start?

Upvotes: 1

Views: 1670

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318518

No, you don't want to run background processes inside your apache/whatever WSGI environment.

Start them on the shell and use some method to communicate with your background process.

Upvotes: 3

Related Questions