私はスレッド内でdjangoのcall_methodを実行したいと思います。 これはサンプルコードです:スレッド内のdjangoのcall_commandに関する問題
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)にコメントすると、
のスレッドしか作成されません。
ありがとうございます!
これは非常に良い方法ではありません。ポート8000を使用するとどうなりますか?あるいは何か他にポート8000が必要な場合はどうしますか?少なくとも、セミランダムポート番号を使用してください。 –
'threading'ではなく' multiprocessing'を使って同じことが起こります。 –