If using mod_wsgi you can do this by configuring a daemon process
group with a single process, specifying that a specific script file
should be run when that process is started, and then simply never
directing any requests to that process.
Thus, the daemon process definition might look like:
WSGIDaemonProcess mybackgroundtasks processes=1 threads=1
We set threads=1 simply so that minimum number of handler threads are
created in pool. The threads will never actually be used as we will
never direct any requests to it.
To have specific code executed when this daemon process is started,
define:
WSGIImportScript /some/path/script.wsgi process-
group=mybackgroundtasks application-group=%{GLOBAL}
In other words, when background tasks daemon process is started, in
the main Python interpreter context load the Python code in /some/path/
script.wsgi. Such code will be executed as if code file was a module
and imported.
To execute a background task, that script file would load threading
module, create a thread and run it with your code. A background thread
is still used as script must return.
And that is it.
Because this is a managed mod_wsgi daemon process, there is no need
for using separate supervisor as Apache/mod_wsgi will manage process
for you. As a result, the process will be started, stopped and
restarted as Apache is triggered to do the same. If the process
crashes for some reason, Apache will restart it automatically.
Note that the above is only necessary if running your WSGI application
if mod_wsgi embedded mode or a multiprocess daemon mode configuration.
That is, where multiple processes. If you were running your WSGI
application in single multithreaded daemon process, you could just the
above in same process as you were using for web application.
Even if WSGI application running in embedded mode or multiprocess
daemon mode configuration, you could still send subset of URLs to this
single background task process. In other words, the background task
process could itself be a mini web application, thus allowing you to
control through the web the background tasks it is performing.
Thus you might have:
WSGIScriptAlias /tasks /some/path/taskapp.wsgi
WSGIScriptAlias / /some/path/mainapp.wsgi
<Location /tasks>
WSGIProcessGroup mybackgroundtasks
</Location>
In summary, you can do what you want with just mod_wsgi, you do not
need to separately exec a back end process managed under supervisor or
similar system.
Graham