开发者

How can I run django with mod_wsgi and use the multiprocess module?

开发者 https://www.devze.com 2023-03-25 00:53 出处:网络
The workflow I am dealing with (user-wise) looks like this: User submits information and files with a form

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

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消