Reputation: 760
I am trying to execute a shell command and kill it using python signal module.
I know signals work only with main thread, so I run the Django development server with,
python manage.py runserver --nothreading --noreload
and it works fine.
But when i deploy the django application with Apache/mod_wsgi, it shows the following error:
[Fri Sep 12 20:07:00 2014] [error] response = function.call(request, **data)
[Fri Sep 12 20:07:00 2014] [error] File "/Site/cloud/lib/python2.6/site-packages/dajaxice/core/Dajaxice.py", line 18, in call
[Fri Sep 12 20:07:00 2014] [error] return self.function(*args, **kwargs)
[Fri Sep 12 20:07:00 2014] [error] File "/Site/cloud/soc/website/ajax.py", line 83, in execute
[Fri Sep 12 20:07:00 2014] [error] data = scilab_run(code, token, book_id, dependency_exists)
[Fri Sep 12 20:07:00 2014] [error] File "/Site/cloud/soc/website/helpers.py", line 58, in scilab_run
[Fri Sep 12 20:07:00 2014] [error] output = task.run().communicate()[0]
[Fri Sep 12 20:07:00 2014] [error] File "/Site/cloud/soc/website/timeout.py", line 121, in run
[Fri Sep 12 20:07:00 2014] [error] lambda sig,frame : os.killpg(self.pgid,self.timeoutSignal) )
[Fri Sep 12 20:07:00 2014] [error] ValueError: signal only works in main thread
Here is my apache virtualhost setting:
WSGIDaemonProcess testcloud display-name=scilab_cloud user=apache group=apache threads=1
WSGIProcessGroup testcloud
WSGIScriptAlias / /Site/cloud/soc/soc/wsgi.py
WSGIImportScript /Site/cloud/soc/soc/wsgi.py process-group=testcloud application-group=%{GLOBAL}
I also have the below settings outside virtualhost in httpd.conf:
WSGIRestrictSignal Off
WSGISocketPrefix /var/run/wsgi
Here is the link to the program which uses signal and the one which I use in my django application.
Any help would be appreciated.
Upvotes: 9
Views: 6660
Reputation: 866
are you running with DEBUG enabled in setting.py ? If yes try disabling it to see if the issue persists.
Upvotes: 0
Reputation: 321
I'm not sure that can be done that easily, or at least not with mod_wsgi. The decision to thread or not to thread is the sum of build and run time options in both apache and mod_wsgi, which are both set by default, to threading.
I would point you to the docs about that, but I can only post two links, so I think it's better to spend them in proposing a solution:
I had decently good experiences running shell commands from python with sh, which even has an asynchronous execution module. Maybe you can start your python code running the shell command, and deal with the callback object when needed.
Or, even better, as sh
asks you to have some cares when handling signals, you could just run it without the asynchronous execution module, but in another process with multiprocessing.Proces, which will give you a Process
object you can just kill with object.terminate()
Upvotes: -1