我正在尝试使用cherrypy + multiprocessing(启动工作进程'进程')+ gevent(从工作进程'进程'内启动并行i/o greenlets)的组合。似乎最简单的方法是monkeypatch multiprocessing,因为greenlets只能在主应用程序进程中运行。
但是,看起来猴子修补程序适用于多处理的某些部分,而不适用于其他部分。这是我的示例 CherryPy 服务器:
from gevent import monkey
monkey.patch_all()
import gevent
import cherrypy
import multiprocessing
def launch_testfuncs():
jobs = [gevent.spawn(testfunc)
for i in range(0, 12)]
gevent.joinall(jobs, timeout=10)
def testfunc():
print 'testing'
class HelloWorld(object):
def index(self):
launch_testfuncs()
return "Hello World!"
index.exposed = True
def index_proc(self):
proc = multiprocessing.Process(target=launch_testfuncs)
proc.start()
proc.join()
return "Hello World 2!"
index_proc.exposed = True
def index_pool(self):
pool = multiprocessing.Pool(1)
return "Hello World 3!"
index_pool.exposed = True
def index_namespace(self):
manager = multiprocessing.Manager()
anamespace = manager.Namespace()
anamespace.val = 23
return "Hello World 4!"
index_namespace.exposed = True
cherrypy.quickstart(HelloWorld())
猴子修补后的以下工作:
index
- 直接从cherrypy类中产生greenletsindex_proc
- 用于multiprocessing.Process
启动一个新进程,然后从该进程中生成 greenlets
以下有问题:
index_pool
- 启动multiprocessing.Pool
-挂起并且永不返回index_namespace
- 初始化multiprocessing.Manager
命名空间以管理池/工人集合中的共享内存 -返回以下错误消息:[15/Nov/2012:17:19:31] HTTP Traceback (most recent call last): File "/Library/Python/2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/Library/Python/2.7/site-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/Library/Python/2.7/site-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "server.py", line 39, in index_namespace anamespace = manager.Namespace() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/managers.py", line 667, in temp token, exp = self._create(typeid, *args, **kwds) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/managers.py", line 565, in _create conn = self._Client(self._address, authkey=self._authkey) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/connection.py", line 175, in Client answer_challenge(c, authkey) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/connection.py", line 414, in answer_challenge response = connection.recv_bytes(256) # reject large message IOError: [Errno 35] Resource temporarily unavailable
我尝试在 gevent docs 中找到一些与此相关的文档,但找不到与此相关的任何内容。只是gevent的猴子补丁不完整吗?有没有其他人有类似的问题,有没有办法解决它?