2012-07-20 76 views
3

問題,所以我一直在試圖使運行在後臺服務器的過程,並與守護進程啓動。所以我的代碼是:與Python-GEVENT和蟒蛇守護

class App(): 
    def __init__(self): 
    self.stdin_path = '/dev/null' 
    self.stdout_path = '/dev/tty' 
    self.stderr_path = '/dev/tty' 
    self.pidfile_path = '/tmp/foo.pid' 
    self.pidfile_timeout = 5 
    def run(self): 
    server = WSGIServer(('localhost',28080),handle_request) 
    server.serve_forever() 

if __name__ == '__main__': 
    app = App() 
    daemon_runner = runner.DaemonRunner(app) 
    daemon_runner.do_action() 

然而,這給我的錯誤:

[warn] Epoll ADD(1) on fd 5 failed. Old events were 0; read change was 1 (add); write change was 0 (none): Invalid argument 
Traceback (most recent call last): 
    File "enrollmentrunner2.py", line 110, in <module> 
    daemon_runner.do_action() 
    File "/usr/lib/python2.7/dist-packages/daemon/runner.py", line 186, in do_action 
    func(self) 
    File "/usr/lib/python2.7/dist-packages/daemon/runner.py", line 131, in _start 
    self.app.run() 
    File "enrollmentrunner2.py", line 105, in run 
    server.serve_forever() 
    File "/usr/lib/python2.7/dist-packages/gevent/baseserver.py", line 188, in serve_forever 
    self.start() 
    File "/usr/lib/python2.7/dist-packages/gevent/baseserver.py", line 149, in start 
    self.start_accepting() 
    File "/usr/lib/python2.7/dist-packages/gevent/server.py", line 99, in start_accepting 
    self._accept_event = core.read_event(self.socket.fileno(), self._do_accept, persist=True) 
    File "core.pyx", line 308, in gevent.core.read_event.__init__ (gevent/core.c:3960) 
    File "core.pyx", line 252, in gevent.core.event.add (gevent/core.c:2952) 
IOError: [Errno 22] Invalid argument 

我在網上找了警告,我到處都找不到它,錯誤並沒有真正給我太多有用的信息。我已經運行在這裏描述https://stackoverflow.com/a/9047339的程序,我只是把它在主等單獨運行的程序,。但是,當我將它們結合起來,似乎把事情搞得一團糟。有人知道這可能是爲什麼嗎?

回答

7

您遇到fork和epoll(或kqueue)之間的不良交互問題。通常,基於epoll的事件循環很難在fork之後可靠地工作,並且最好重新創建一個新的事件循環。

有幾個方法可以解決您的問題:

  • Upgrade to gevent 1.0.不像GEVENT 0.X它創建的事件循環第一次使用時,而不是在第一進口,從而避免了這個問題。它也使用libev,它比libevent更好地處理fork,雖然不是100%可靠的。在您升級gevent後,您的問題很有可能會消失。

  • 延遲導入gevent包直到分叉後。