线程内 django 的 call_command 问题

发布于 2024-10-06 21:40:14 字数 1256 浏览 4 评论 0原文

我想在线程内执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-10-13 21:40:14

我找到了一个解决方案(克里斯·摩根是对的)。这段代码按我想要的方式工作:

import sys
sys.path.append("/my/django/project/path/")
import threading

# Import my django project configuration settings
from django.core.management import setup_environ, ManagementUtility

from mydjangoprojectname import settings
setup_environ(settings)


class ServerStarter(threading.Thread):
    def __init__(self):
        super(ServerStarter, self).__init__()
        print "ServerStarter instance created"

    def run(self):
        print "Starting Django Server..."
        utility = ManagementUtility()
        command = utility.fetch_command('runserver')
        command.execute(use_reloader=False)


if __name__ == '__main__':
    starter = ServerStarter()
    starter.start()

I found a solution (Chris Morgan was right). This code works as I want:

import sys
sys.path.append("/my/django/project/path/")
import threading

# Import my django project configuration settings
from django.core.management import setup_environ, ManagementUtility

from mydjangoprojectname import settings
setup_environ(settings)


class ServerStarter(threading.Thread):
    def __init__(self):
        super(ServerStarter, self).__init__()
        print "ServerStarter instance created"

    def run(self):
        print "Starting Django Server..."
        utility = ManagementUtility()
        command = utility.fetch_command('runserver')
        command.execute(use_reloader=False)


if __name__ == '__main__':
    starter = ServerStarter()
    starter.start()
童话里做英雄 2024-10-13 21:40:14

我认为这可能是由于 Django 内部服务器按照惯例重新加载所有模块造成的。尝试与 call_command 等效的 --noreload (可能是 call_command("runserver", noreload=True) 但我不确定)。

(另外,QThreadQApplication.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 for call_command (probably call_command("runserver", noreload=True) but I'm not sure).

(Also QThreads are started by QApplication.exec_(); unless you have a special requirement to start it earlier, I don't believe you should run starter.start() yourself.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文