简体   繁体   English

ValueError:信号仅适用于主线程 - Django - mod_wsgi

[英]ValueError: signal only works in main thread - Django - mod_wsgi

I am trying to execute a shell command and kill it using python signal module.我正在尝试执行一个 shell 命令并使用 python信号模块杀死它。

I know signals work only with main thread, so I run the Django development server with,我知道信号只适用于主线程,所以我运行 Django 开发服务器,

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:但是当我使用 Apache/mod_wsgi 部署 django 应用程序时,它显示以下错误:

[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:这是我的 apache 虚拟主机设置:

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:我在 httpd.conf 中的 virtualhost 之外还有以下设置:

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.这是使用信号的程序和我在我的 Django 应用程序中使用的程序的链接

Any help would be appreciated.任何帮助,将不胜感激。

are you running with DEBUG enabled in setting.py ?您是否在 setting.py 中启用了 DEBUG 运行? If yes try disabling it to see if the issue persists.如果是,请尝试禁用它以查看问题是否仍然存在。

I'm not sure that can be done that easily, or at least not with mod_wsgi.我不确定这是否可以轻松完成,或者至少不能使用 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.线程或不线程的决定是 apache 和 mod_wsgi 中构建和运行时选项的总和,默认情况下都设置为线程。

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.我在使用sh从 python 运行 shell 命令方面有很好的经验,它甚至有一个异步执行模块。 Maybe you can start your python code running the shell command, and deal with the callback object when needed.也许您可以启动运行 shell 命令的 Python 代码,并在需要时处理回调对象。

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()或者,更好的是,因为sh要求您在处理信号时要小心,您可以在没有异步执行模块的情况下运行它,但是在另一个带有multiprocessing.Proces 的进程中,它将为您提供一个Process对象,您可以使用object.terminate()杀死它object.terminate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM