如何在python multiProcess上粉碎socket对象?它提出了EOFError

时间:2015-10-11 12:31:54

标签: python sockets multiprocessing

我正在尝试使用简单代码测试multiProcess Procee类。

但是当我将socket对象作为参数移交给Process时,它会引发EOFError

下面是简单的代码和例外。

import multiprocessing
import socket

class Multi(multiprocessing.Process):

    def __init__(self, s):
        multiprocessing.Process.__init__(self)
        self.s = s

    def run(self):
        for i in range(100):
            print "aaaa"


if __name__ in "__main__":
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    p = Multi(s)
    p.start()
    p.join()

Traceback (most recent call last):
  File "C:\Users\sangmin\workspace\multiProcessTest\multiProcessTest.py", line 24, in <module>
    p.start()
  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\multiprocessing\forking.py", line 277, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:\Python27\lib\multiprocessing\forking.py", line 199, 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 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 548, in save_tuple
    save(element)
  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 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <built-in method recvfrom_into of _socket.socket object at 0x020C94A0>: it's not found as __main__.recvfrom_into
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\multiprocessing\forking.py", line 381, 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

1 个答案:

答案 0 :(得分:1)

在Windows中,multiprocessing使用pickle在进程之间传输对象。套接字对象无法被腌制,因此您会看到问题。

您的代码在Linux中有效,这是因为multiprocessing在该平台上使用fork。分叉进程的子进程继承父进程的文件句柄,其中一个是套接字。

目前尚不清楚你想要实现的目标。您可以将主机名和端口信息传递给子进程,并让它创建自己的套接字。

您还应该能够使用threading代替multiprocessing