我会在线程中执行 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) ,那么只会
创建一个线程(这就是我想要的)。
提前致谢!