分叉在Django

时间:2012-11-26 12:20:12

标签: python django linux

我正在尝试在django视图中生成os.fork()并收到错误:Port 8000 already in use。据我所知,当我os.fork()文件描述符继承时,我在两个进程中的同一个端口中得到两个套接字。

   def view(request):
       ...
       if not os.fork():
          target()
       return

    def target(*args, **kwargs):
       ...
       sd = socket.socket(*args, **kwargs)
       sd.bind(*args)
       sd.listen()
       nsd = sd.accept()
      ...

如果我在子项中为已打开的描述符执行os.close(),则会收到WSGI错误:

for i in os.listdir("/proc/%s/fd" % os.getpid()):
   os.close(int(i))

Traceback (most recent call last):
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 94, in run
    self.finish_response()
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 135, in finish_response
    self.write(data)
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 218, in write
    self.send_headers()
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 274, in send_headers
    self.send_preamble()
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 197, in send_preamble
    self._write('HTTP/%s %s\r\n' % (self.http_version,self.status))
  File "/usr/lib/python2.6/wsgiref/handlers.py", line 404, in _write
    self.stdout.write(data)
  File "/usr/lib/python2.6/socket.py", line 318, in write
    self.flush()
  File "/usr/lib/python2.6/socket.py", line 297, in flush
    self._sock.sendall(buffer(data, write_offset, buffer_size))
error: [Errno 9] Bad file descriptor

我需要将这个exaclty作为单独的过程,我不想使用像芹菜这样的三维派对应用程序。

1 个答案:

答案 0 :(得分:1)

我认为正确的解决方案是使用芹菜(http://docs.celeryproject.org/en/latest/)或同等的。