Thread 内で 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 が2 回作成されます。
そして、両方の ServerStarter のインスタンスが実行されます。
run メソッドで call_command("runserver", noreload=True) をコメントすると、
スレッドが 1 つだけ作成されます (それが私の望みです)。
前もって感謝します!