线程内 django 的 call_command 问题
我想在线程内执行 django 的 call_method 。 这是示例代码:
import sys
sys.path.append("/my/django/project/path/")
import threading
import time
# Import my django project configuration settings
from django.core.management import setup_environ
from mydjangoprojectname import settings
setup_environ(settings)
from django.core.management import call_command
class ServerStarter(threading.Thread):
def __init__(self):
super(ServerStarter, self).__init__()
print "ServerStarter instance created"
def run(self):
print "Starting Django Server..."
call_command("runserver", noreload=True)
if __name__ == '__main__':
starter = ServerStarter()
starter.start()
------------------------------
OutPut:
ServerStarter instance created
Starting Django Server...
ServerStarter instance created
Starting Django Server...
Validating models...
0 errors found
Django version 1.2.3, using settings 'mydjangoprojectname.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Django 服务器正确启动,但 ServerStarter 创建了两次。
并且两个 ServerStarter 实例都运行。
如果我在 run 方法中注释 call_command("runserver", noreload=True) ,那么只有
创建一个线程(这就是我想要的)。
提前致谢!
I would to execute the django's call_method inside a Thread.
This is the sample code:
import sys
sys.path.append("/my/django/project/path/")
import threading
import time
# Import my django project configuration settings
from django.core.management import setup_environ
from mydjangoprojectname import settings
setup_environ(settings)
from django.core.management import call_command
class ServerStarter(threading.Thread):
def __init__(self):
super(ServerStarter, self).__init__()
print "ServerStarter instance created"
def run(self):
print "Starting Django Server..."
call_command("runserver", noreload=True)
if __name__ == '__main__':
starter = ServerStarter()
starter.start()
------------------------------
OutPut:
ServerStarter instance created
Starting Django Server...
ServerStarter instance created
Starting Django Server...
Validating models...
0 errors found
Django version 1.2.3, using settings 'mydjangoprojectname.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Django server starts correctly, but ServerStarter is created twice.
And both ServerStarter's instances run.
If I comment call_command("runserver", noreload=True) in the run method, then only
one thread is created (and that is what I want).
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个解决方案(克里斯·摩根是对的)。这段代码按我想要的方式工作:
I found a solution (Chris Morgan was right). This code works as I want:
我认为这可能是由于 Django 内部服务器按照惯例重新加载所有模块造成的。尝试与
call_command
等效的--noreload
(可能是call_command("runserver", noreload=True)
但我不确定)。(另外,
QThread
由QApplication.exec_()
启动;除非您有特殊要求提前启动它,否则我不认为您应该运行starter .start()
你自己。)I think this is probably caused by the Django internal server reloading all modules as is its wont. Try whatever the equivalent of
--noreload
is forcall_command
(probablycall_command("runserver", noreload=True)
but I'm not sure).(Also
QThread
s are started byQApplication.exec_()
; unless you have a special requirement to start it earlier, I don't believe you should runstarter.start()
yourself.)