将ServerProxy对象返回给线程/进程。可能吗?

时间:2014-03-05 09:26:09

标签: python multithreading multiprocessing xml-rpc

为什么我不能从进程池执行的函数中返回ServerProxy对象?

这有效:

def function():
    server=initserver()

def initserver():
    serv=xmlrpclib.ServerProxy("http://server:6060/RPC2")
    return serv

然而,这不是:

def function():
    pool = Pool (processes=4) 
    results=[pool.apply(initserver, args=()) for i in range (0, 4)
    pool.close()        
    pool.join()

def initserver():
    serv=xmlrpclib.ServerProxy("http://server:6060/RPC2")
    return serv

我知道我必须“获取”“结果”中的值,但我没有机会。在执行“返回服务”的那一刻,程序停止并输出以下内容(对于池的每个进程都有一个这样的块),我必须手动终止它:

Process PoolWorker-4:
Traceback (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 99, in worker
put((job, i, result))
File "/usr/lib/python2.7/multiprocessing/queues.py", line 390, in put
return send(obj)
File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request
verbose=self.__verbose
File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request
return self.single_request(host, handler, request_body, verbose)
File "/usr/lib/python2.7/xmlrpclib.py", line 1297, in single_request
return self.parse_response(response)
File "/usr/lib/python2.7/xmlrpclib.py", line 1473, in parse_response
return u.close()
File "/usr/lib/python2.7/xmlrpclib.py", line 793, in close
raise Fault(**self._stack[0])
Fault: <Fault -506: "Method '__getinitargs__' not defined">

¿想法?

我可以为每个进程创建一个全局变量“serv”(实际上这就是我处理这个问题的方式),但是还有另一个问题:

Concurrent XMLRPC-C calls with global ServerProxy object not working as expected

0 个答案:

没有答案