我试图os.fork()
在 django 视图中制作并得到一个错误: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
我需要将此作为单独的过程进行,并且我不想使用 celery 等 3-d 派对应用程序。