在 wsgi 上与 django 项目并行的正确方法

发布于 2024-09-04 00:23:34 字数 330 浏览 10 评论 0原文

我正在编写一个 django 项目,我需要有一个执行某些任务的并行线程。

该项目将使用mod_wsgi部署在Apache2.2中。

实际上,我的实现由一个带有 while True - Sleep 的线程组成,该线程是从我的 django.wsgi 文件调用的。

这个实现正确吗?

提出了两个问题: django.wsgi 只被调用一次吗?我会只运行该线程的实例吗?其次,我需要“手动”访问至少一个页面才能运行线程。有解决方法吗?

有人对更好的解决方案有一些提示吗?

提前致谢。

I'm writing a django project, and I need to have a parallel thread which performs certain tasks.

The project will be deployed in Apache2.2 with mod_wsgi.

Actually my implementation consists on a thread with a while True - Sleep which is called from my django.wsgi file.

Is this implementation correct?

Two problems raises: does django.wsgi get called only once? Will I have just that instance of the thread running? And second, I need to "manually" visit at least a page to have the Thread run. Is there a workaround?

Does anyone has some hints on better solutions?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

弥繁 2024-09-11 00:23:34

至于您要求必须发出请求来触发线程创建的解决方法,您有两种选择。第一种是使用 WSGIImportScript 指令在启动时预加载 WSGI 脚本文件。第二个是使用 WSGIScriptAlias 的进程组和应用程序组选项来实现相同的目的。

优选第二种方式。您还需要确保使用守护进程模式,这样如果 WSGI 脚本文件发生更改,整个进程就会重新加载。如果您使用嵌入模式并且 WSGI 脚本文件发生更改,则只需重新加载脚本即可创建第二个后台线程。

因此,对于守护进程模式使用:

WSGIDaemonProcess mysite
WSGIScriptAlias / /some/path/django.wsgi process-group=mysite application-group=%{GLOBAL}

进程组和应用程序组选项均显式提供给 WSGIScriptAlias,这会在任何请求到达之前在进程启动时预加载 WSGI 脚本文件,从而产生副作用。

请注意,您必须使用 mod_wsgi 3.0 或更高版本,才能为 WSGIScriptAlias 提供选项来实现此目的。


更新 1

如果您使用的是 Windows 并且无权访问守护进程模式:

WSGIImportScript /some/path/django.wsgi application-group=%{GLOBAL}
WSGIScriptAlias / /some/path/django.wsgi application-group=%{GLOBAL}

似乎是 mod_wsgi 中的一个遗漏,当守护进程模式不可用时,仅为 WSGIScriptAlias 设置应用程序组,仍然会导致预加载。

As to your request for workaround for having to make a request to trigger creation of thread, you have two options. The first is to use the WSGIImportScript directive to preload your WSGI script file at startup. The second being to achieve the same thing using process-group and application-group options to WSGIScriptAlias.

The second way is preferred. You also want to make sure you are using daemon mode such that if WSGI script file is changed that whole process is reloaded. If you use embedded mode and WSGI script file changed, just the script is reloaded and you will create a second background thread.

Thus, for daemon mode use:

WSGIDaemonProcess mysite
WSGIScriptAlias / /some/path/django.wsgi process-group=mysite application-group=%{GLOBAL}

That process-group and application-group options are both explicitly supplied to WSGIScriptAlias has side effect of preloading WSGI script file on process start before any requests have arrived.

Note, you must be using mod_wsgi 3.0 or later for theis latter ability of supplying options to WSGIScriptAlias to achieve this.


UPDATE 1

If you are on Windows and don't have access to daemon mode:

WSGIImportScript /some/path/django.wsgi application-group=%{GLOBAL}
WSGIScriptAlias / /some/path/django.wsgi application-group=%{GLOBAL}

Seems to be an omission in mod_wsgi that setting just application-group for WSGIScriptAlias when daemon mode not available, still results in preloading.

⊕婉儿 2024-09-11 00:23:34

为什么需要它在线程中运行?如果您想要一个单独的长时间运行的进程,请从命令行或守护程序创建一个进程。或者,使用类似 celery 的东西。

Why do you need this to run in a thread? If you want a separate long-running process, then create one from the command line or a daemon. Or, use something like celery.

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