Python 这泡菜为什么不吃?

Python 这泡菜为什么不吃?,python,pickle,Python,Pickle,我试图将其作为一个来自多处理的进程来运行,但是当启动线程时,pickler掉了下来,我不知道是什么阻止了它的酸洗。我试着注释掉套接字代码和消息obj代码,但仍然不起作用-我做错了什么 class TransmitThread(Process): def __init__(self, send_queue, reply_queue, control_pipe, recv_timeout=2, buffer_size=4096): """ This

我试图将其作为一个来自多处理的进程来运行,但是当启动线程时,pickler掉了下来,我不知道是什么阻止了它的酸洗。我试着注释掉套接字代码和消息obj代码,但仍然不起作用-我做错了什么

class TransmitThread(Process):

    def __init__(self, send_queue, reply_queue, control_pipe, recv_timeout=2, buffer_size=4096):
        """
            This init function is called when the thread is created. Function simply calls the Process class init
            function, and stores the class vars.
        """
        # Call process class init
        Process.__init__(self)

        # Store class vars
        self.send_queue     = send_queue
        self.reply_queue    = reply_queue
        self.control_pipe   = control_pipe
        self._recv_timeout  = recv_timeout
        self._buffer_size   = buffer_size

    def run(self):
        """
            This is the main function that is called when the thread is started.
            The function loops forever, waiting for a send message in the queue, and processes the message to send
            and fetches the response. The thread loops forever until it's terminated or the KILL THREAD command is
            passed through the control pipe.
        """
        # Start our forever running loop
        while True:

            # Check if there is anything in the pipe
            if self.control_pipe.poll():
                # Check if we received the kill thread command
                if self.control_pipe.recv() == KILL_THREAD_COMMAND:
                    # Kill the while loop and end the thread
                    break

            # Check if there is anything in message queue
            if not self.send_queue.empty():
                # Fetch message from the queue to send, and unpickle
                message_obj, message_pickle = self.send_queue.get()

                # Open socket and set timeout
                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                sock.settimeout(self._recv_timeout)

                # Connect socket to the recipient
                sock.connect( message_obj.recipient_address )

                # Push the pickled message down the socket
                sock.sendall(message_pickle)

                # Check if the message we send is a request (should get a response)
                if str(message_obj.message_type) == str(Message.REQUEST):

                    print "fetching reply"

                    # Lets fetch the response, and push the pickled message onto the queue
                    self.reply_queue.put( sock.recv(self._buffer_size) )

                    print "got a reply"

                # All done, close the socket
                sock.close()

            # Add small delay to stop this thread consuming too much CPU time
            sleep(0.1)

错误消息是:

File "C:/Users/oliver/OneDrive/GIT/pyke/pyke.py", line 127, in __init__
    self.thread.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 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\pickle.py", line 725, in save_inst
    save(stuff)
  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 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 396, in save_reduce
    save(cls)
  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 <type 'thread.lock'>: it's not found as thread.lock
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
文件“C:/Users/oliver/OneDrive/GIT/pyke/pyke.py”,第127行,在__
self.thread.start()
文件“C:\Python27\lib\multiprocessing\process.py”,第130行,在开始处
self.\u popen=popen(self)
文件“C:\Python27\lib\multiprocessing\forking.py”,第277行,在\uuu init中__
转储(进程对象、到子进程、最高\u协议)
文件“C:\Python27\lib\multiprocessing\forking.py”,第199行,在转储中
ForkingPickler(文件、协议).dump(obj)
文件“C:\Python27\lib\pickle.py”,第224行,位于转储文件中
自我保存(obj)
文件“C:\Python27\lib\pickle.py”,第331行,保存
自我保存(obj=obj,*rv)
文件“C:\Python27\lib\pickle.py”,第419行,在save\u reduce中
保存(状态)
文件“C:\Python27\lib\pickle.py”,第286行,保存
f(self,obj)#用显式self调用未绑定方法
保存目录中第649行的文件“C:\Python27\lib\pickle.py”
self.\u batch\u setitems(obj.iteritems())
文件“C:\Python27\lib\pickle.py”,第681行,在批处理设置项中
保存(v)
文件“C:\Python27\lib\pickle.py”,第286行,保存
f(self,obj)#用显式self调用未绑定方法
文件“C:\Python27\lib\pickle.py”,第725行,在save\u inst中
保存(东西)
文件“C:\Python27\lib\pickle.py”,第286行,保存
f(self,obj)#用显式self调用未绑定方法
保存目录中第649行的文件“C:\Python27\lib\pickle.py”
self.\u batch\u setitems(obj.iteritems())
文件“C:\Python27\lib\pickle.py”,第681行,在批处理设置项中
保存(v)
文件“C:\Python27\lib\pickle.py”,第331行,保存
自我保存(obj=obj,*rv)
文件“C:\Python27\lib\pickle.py”,第419行,在save\u reduce中
保存(状态)
文件“C:\Python27\lib\pickle.py”,第286行,保存
f(self,obj)#用显式self调用未绑定方法
保存目录中第649行的文件“C:\Python27\lib\pickle.py”
self.\u batch\u setitems(obj.iteritems())
文件“C:\Python27\lib\pickle.py”,第681行,在批处理设置项中
保存(v)
文件“C:\Python27\lib\pickle.py”,第331行,保存
自我保存(obj=obj,*rv)
文件“C:\Python27\lib\pickle.py”,第396行,在save\u reduce中
保存(cls)
文件“C:\Python27\lib\pickle.py”,第286行,保存
f(self,obj)#用显式self调用未绑定方法
文件“C:\Python27\lib\pickle.py”,第748行,在save\u global中
(对象、模块、名称))
pickle.PicklingError:无法pickle:未将其作为thread.lock找到
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\multiprocessing\forking.py”,第381行,在main中
self=加载(从父级)
加载文件“C:\Python27\lib\pickle.py”,第1378行
返回Unpickler(file.load())
加载文件“C:\Python27\lib\pickle.py”,第858行
调度[键](自身)
文件“C:\Python27\lib\pickle.py”,第880行,在load\u eof中
提高采收率
伊奥费罗

pickler错误来自于
多处理。进程试图在内部将自身pickle到子进程。我很确定您的一个实例变量没有正确地pickle到子进程。你的问题不清楚哪一个

    # Store class vars
    self.send_queue     = send_queue
    self.reply_queue    = reply_queue
    self.control_pipe   = control_pipe
    self._recv_timeout  = recv_timeout
    self._buffer_size   = buffer_size
[在OP的评论之后编辑]:

问题是
send\u queue
reply\u queue
queue.queue
s而不是
multiprocessing.queue
。分支子工作进程时,
进程
尝试将自身和任何实例变量序列化到子工作进程。但是,
Queue.Queue
s是不可序列化的本地对象,因此会出现错误

与此问题相关的另一个事实是,
multiprocessing.Queue
来自
Queue
,而无需重新导出它们。这一点,尽管有点隐藏在混乱之中:

注意:
多处理
使用通常的
队列。空的
队列。满的
异常来表示超时。它们在
多处理
命名空间中不可用,因此需要从
队列
导入它们


这些消息看起来怎么样?来自pickler的错误消息是什么?故障发生在哪里?你确定,你已经有了完整的消息(
recv()
可能会返回短消息)?我已经添加了pickler中的错误消息。我试过注释recv()调用和所有套接字函数,但仍然不起作用。大约一个小时前我就开始工作了,但我无法计算出我将它更改为什么。我的
线程
模块也没有
线程。锁
。所以,也许您已经设置了一些不同的路径来修改python所知的
thread
module?顺便说一句,您应该提到您的python版本。
Thread.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuself)多处理
线程
模块?@dhke抱歉,这是一个输入错误。我已经切换回Thread类进行测试,因为这是可行的。你是对的,但是这是一个打字错误,这比什么都重要。在测试期间,我在线程和进程之间切换,因为线程一直工作,而进程不工作。正如您所期望的,我正在从多处理导入进程,从线程导入线程。解决这个问题并不能解决问题:(哼,我现在有点困惑。回复和发送队列是Queue()对象。我用它们在进程和产生它的父进程之间来回推送数据。我做错了什么?所以父进程有队列obj,进程有它,而你在两者之间推送数据。这就是进程之间的数据链接-这不正是队列应该被使用的方式吗?@Oliver我不知道