1
import multiprocessing
import time
import pycurl

class Bot(multiprocessing.Process):
    def __init__(self):
        multiprocessing.Process.__init__(self)       
        self.c = pycurl.Curl() 

    def run(self):
        pass

if __name__ == '__main__': 
    Bot().start()    

此代码在 Ubuntu 13.04 x64 上运行良好,但在 Windows 7 x64/Server 2008 x64 上失败。我使用 pyCurl 7.19.0 和 Python 2.7。错误是:

Z:\bot>python test.py
Traceback (most recent call last):
  File "test.py", line 74, in <module>
    Bot().start()
  File "C:\Python27\lib\multiprocessing\process.py", line 104, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\multiprocessing\forking.py", line 244, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:\Python27\lib\multiprocessing\forking.py", line 167, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:\Python27\lib\pickle.py", line 419, in save_reduce
    save(state)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 313, in save
    (t.__name__, obj))
pickle.PicklingError: Can't pickle 'Curl' object: <pycurl.Curl object at 0x00000
00002360478>

Z:\bot>Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\multiprocessing\forking.py", line 347, in main
    self = load(from_parent)
  File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 880, in load_eof
    raise EOFError
EOFError

为什么会发生这种情况?

4

1 回答 1

1

在 posix 系统上,multiprocessing使用fork()syscall 工作,它克隆正在运行的进程及其所有状态(包括复杂类的实例,例如pycurl.Curl)。

Windows 没有fork(). 或类似的东西,因此multiprocessing为每个子进程启动了一个新的 python 解释器,并带有一个特殊的存根函数,该函数监听父进程并重新创建状态,使其看起来很像它曾经fork()被使用过的状态。为此使用的关键技术multiprocessing是,它在使用父进程时从父进程重新创建子进程中的每个对象。这通过将对象转换为字节码表示(使用pickle模块),通过管道将它们发送给子对象,然后将它们转换回 python 对象。

对于大多数类型的 python 对象,这可以完美地工作,并且对您来说是透明的。有许多明显的事情是不可能奏效的;打开的文件不能以这种方式传递;非python库对象的状态也不能,它们对系统一无所知pickle。不幸pycurl的是,这两者都有点。

于 2013-05-20T14:05:03.737 回答