Python:Django: Signal handler and main thread

codeJack picture codeJack · Dec 20, 2011 · Viewed 9k times · Source

I am building a django application which depends on a python module where a SIGINT signal handler has been implemented.

Assuming I cannot change the module I am dependent from, how can I workaround the "signal only works in main thread" error I get integrating it in Django ?

Can I run it on the Django main thread? Is there a way to inhibit the handler to allow the module to run on non-main threads ?

Thanks!

Answer

atereshkin picture atereshkin · Jan 3, 2013

Django's built-in development server has auto-reload feature enabled by default which spawns a new thread as a means of reloading code. To work around this you can simply do the following, although you'd obviously lose the convenience of auto-reloading:

python manage.py runserver --noreload

You'll also need to be mindful of this when choosing your production setup. At least some of the deployment options (such as threaded fastcgi) are certain to execute your code outside main thread.