为什么我的多处理Python脚本在Linux上运行,但在Windows上遇到与pickle相关的异常?

时间:2017-07-01 16:01:11

标签: linux windows pickle python-multiprocessing python-sockets

我觉得这与Windows中没有fork()有关,但我不确定做什么关于它......

我有一个名为workObj的类,它从多处理继承Process。在Windows上,创建此类的新实例很顺利,但调用实例的start()方法会引发以下异常:

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 1384, in load
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 864, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 1089, in load_newobj
    obj = cls.__new__(cls, *args)
TypeError: __new__() takes at least 2 arguments (1 given)

为什么呢?我必须采取哪些措施来解决它?它试图腌制的数据应该只是字符串,这是可选择的,对吧?即便如此,如果我试图挑选一种不可摧毁的数据类型,那么Linux上也不应该失败吗?

workObj包含一个无限循环,它通过python套接字扩展到服务器,检索字符串,处理它,然后将其发送回服务器。

class workObj(Process):
        def __init__(self, name, ip, port, keyFile, *args, **kwargs):
                Process.__init__(self)
                global printer
                self.tprint = printer.tprint            # We'll hold a reference to printer internally.
                self.workCounter = 0
                self.ip = ip
                self.port = port
                self.conn = socket.socket()
                self.workDone = False
                self.connected = True
                self.conn.settimeout(10)
                self.secconn = ssl.wrap_socket(self.conn, ca_certs=keyFile, cert_reqs=ssl.CERT_REQUIRED)
                self.name = name
                try:
                        self.secconn.connect((self.ip, self.port))
                except:
                        printer.tprint(self.name, "Fatal error in constructor: workObj {}: connect to {}:{} failed! WARNING! Object is unusable!".format(self.name, self.ip, self.port), error= True)
                        self.workDone = True
                        self.connected = False
                        # raise                         # Come back later and check this syntax.
        def getWorkCount(self):
                return self.workCounter
        def isConnected(self):
                return self.connected
        def finishUp(self):
                self.workDone = True
        def run(self):
                s = self.secconn
                tprint = self.tprint
                try:
                        while not self.workDone:
                                workStr = s.recv(128)
                                if workStr == "":
                                        self.workDone = True
                                        tprint(self.name, "Got blank job from server.")
                                        break
                                sendStr = doWork(workStr)
                                s.send(sendStr)
                                #s.send(doWork(s.recv(128)))
                                self.workCounter += 1
                except socket.timeout as e:
                        tprint(self.name, "Timeout getting or sending work. Assuming server broke.", error= True)
                        tprint(self.name, str(e), error=True)
                        self.workDone = True
                except KeyboardInterrupt as e:
                        tprint(self.name, "Caught keyboard interrupt, sending server blank job.", error=True)
                        s.send("")
                        self.workDone = True
                except:
                        # Literally anything else?
                        import sys
                        e = sys.exc_info()[:]
                        tprint(self.name, "workObj {} broke.".format(self.name), error=True)
                        tprint(self.name, "{}\n{}".format(e[1], e[2]), error=True)
                        self.workDone = True
                        #raise
                s.close()
                self.connected = False

0 个答案:

没有答案