如何使用 mod_wsgi 运行 django 并使用多进程模块?

发布于 2024-11-27 21:11:01 字数 1197 浏览 1 评论 0原文

我正在处理的工作流程(用户方面)如下所示:

  • 用户使用表单提交信息和文件 保存
  • 表单 完成
  • 其他保存后处理

这很好,但保存后处理需要相当长的时间,所以我我希望在后台执行此操作,并向消息发出 HttpResponseRedirect,通知用户处理正在进行,请稍后返回。不幸的是,这似乎不起作用;我现在得到的是这样的:

    if form.is_valid():
        p = multiprocessing.Process(target=form.save)
        p.start()
        return HttpResponseRedirect('/running')

但是我得到的错误是这样的:

IOError at /content/script/new/
sys.stdout access restricted by mod_wsgi
...
/usr/lib/python2.6/multiprocessing/forking.py in __init__
    # We define a Popen class similar to the one from subprocess, but
    # whose constructor takes a process object as its argument.
    #
    class Popen(object):
        def __init__(self, process_obj):
    >>>>        sys.stdout.flush() ...
            sys.stderr.flush()
            self.returncode = None
            self.pid = os.fork()
            if self.pid == 0:
                if 'random' in sys.modules:
▼ Local vars
Variable    Value
process_obj 
<Process(Process-1, initial)>
self    
<multiprocessing.forking.Popen object at 0xb8a06dec>

python 有更神奇的方法来做到这一点吗?姜戈有吗?如果没有,我怎样才能继续使用多重处理?

The workflow I am dealing with (user-wise) looks like this:

  • User submits information and files with a form
  • Form is saved
  • Additional post-save processing is done

This is fine, but the post-save processing takes quite a while, so I'm looking to do it in the background and issue an HttpResponseRedirect to a message informing the user that processing is happening and to please return later. Unfortunately, this doesn't seem to be working; what I've got at the moment is this:

    if form.is_valid():
        p = multiprocessing.Process(target=form.save)
        p.start()
        return HttpResponseRedirect('/running')

But the error that I get back is this:

IOError at /content/script/new/
sys.stdout access restricted by mod_wsgi
...
/usr/lib/python2.6/multiprocessing/forking.py in __init__
    # We define a Popen class similar to the one from subprocess, but
    # whose constructor takes a process object as its argument.
    #
    class Popen(object):
        def __init__(self, process_obj):
    >>>>        sys.stdout.flush() ...
            sys.stderr.flush()
            self.returncode = None
            self.pid = os.fork()
            if self.pid == 0:
                if 'random' in sys.modules:
▼ Local vars
Variable    Value
process_obj 
<Process(Process-1, initial)>
self    
<multiprocessing.forking.Popen object at 0xb8a06dec>

Does python have a more magical way to do this? Does Django? If not, how can I go ahead and use multiprocessing?

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

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

发布评论

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

评论(2

抱着落日 2024-12-04 21:11:01

使用 celery

mod_wsgi 环境可以是多线程的——具体取决于您的配置。您不想干扰 Apache、mod_wsgi 和 Django 已经使用线程和进程来管理 Web 服务器吞吐量的方式。

你必须假设你的 Django 操作是一个单线程,除了尽快响应 Apache 之外不能做任何事情。

Use celery.

The mod_wsgi environment can multi-threaded -- depending on your configuration. You do not want to interfere with how Apache, mod_wsgi, and Django are already using threads and processes to manage web server throughput.

You have to assume that your Django operation is a single thread and cannot do anything except respond to Apache as quickly as possible.

请帮我爱他 2024-12-04 21:11:01

该错误是因为您使用的是旧的 mod_wsgi 版本,而且还因为您尚未阅读:

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Writing_To_Standard_Output
http://blog.dscpl.com .au/2009/04/wsgi-and-printing-to-standard-output.html

正如其他人所说,无论如何,您最好使用 Celery 之类的东西并在 Apache 进程之外触发此类内容。

The error is because you are using an old mod_wsgi version and also because you haven't read:

http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Writing_To_Standard_Output
http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html

As someone else said, you are better off using something like Celery anyway and trigger such stuff outside of Apache processes.

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