zoukankan      html  css  js  c++  java
  • python2,socket多进程的错误pickle.PicklingError: Can't pickle

    python2,socket多进程的错误pickle.PicklingError: Can't pickle

    源码:
    #coding:utf-8
    import socket
    import pickle
    from multiprocessing import Process
    
    HTML_ROOT_DIR = ""
    
    def handle_client(client_socket):
        """处理客户端请求"""
    # 获取客户端请求数据
    request_data = client_socket.recv(1024)
        print("request data:",request_data)
    
        # 构造响应数据
    response_start_line = "HTTP/1.1 200 OK
    "
        response_headers = "Server:My server
    "
        response_body = "hello lijizne"
        response = response_start_line + response_headers + "
    " + response_body
        print("response data:",response)
    
        # 向客户端返回响应数据
    client_socket.send(response)
    
        # 关闭客户端
    client_socket.close()
    
    if __name__ == "__main__":
        server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        server_socket.bind(("",8000))
        server_socket.listen(128)
    
        while True:
            client_socket,client_address = server_socket.accept()
            # print("[%s, %s]用户连接上了" % (client_address[0],client_address[1]))
    print("[%s, %s]用户连接上了" % client_address)
            handle_client_process = Process(target=handle_client,args=(client_socket,))
            handle_client_process.start()
            client_socket.close()

    报错:

    Traceback (most recent call last):
      File "F:/pythontest/pythonsocket/01_static_web_server.py", line 37, in <module>
        handle_client_process.start()
      File "E:Python2libmultiprocessingprocess.py", line 130, in start
        self._popen = Popen(self)
      File "E:Python2libmultiprocessingforking.py", line 277, in __init__
        dump(process_obj, to_child, HIGHEST_PROTOCOL)
      File "E:Python2libmultiprocessingforking.py", line 199, in dump
        ForkingPickler(file, protocol).dump(obj)
      File "E:Python2libpickle.py", line 224, in dump
        self.save(obj)
      File "E:Python2libpickle.py", line 331, in save
        self.save_reduce(obj=obj, *rv)
      File "E:Python2libpickle.py", line 419, in save_reduce
        save(state)
      File "E:Python2libpickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "E:Python2libpickle.py", line 649, in save_dict
        self._batch_setitems(obj.iteritems())
      File "E:Python2libpickle.py", line 681, in _batch_setitems
        save(v)
      File "E:Python2libpickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "E:Python2libpickle.py", line 548, in save_tuple
        save(element)
      File "E:Python2libpickle.py", line 331, in save
        self.save_reduce(obj=obj, *rv)
      File "E:Python2libpickle.py", line 419, in save_reduce
        save(state)
      File "E:Python2libpickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "E:Python2libpickle.py", line 548, in save_tuple
        save(element)
      File "E:Python2libpickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "E:Python2libpickle.py", line 649, in save_dict
        self._batch_setitems(obj.iteritems())
      File "E:Python2libpickle.py", line 681, in _batch_setitems
        save(v)
      File "E:Python2libpickle.py", line 286, in save
        f(self, obj) # Call unbound method with explicit self
      File "E:Python2libpickle.py", line 748, in save_global
        (obj, module, name))
    pickle.PicklingError: Can't pickle <built-in method recvfrom_into of _socket.socket object at 0x0000000002562370>: it's not found as __main__.recvfrom_into

    解决:

    在stackoverflow上找到了答案,是因为windows操作系统的原因,在Windows中,多进程multiprocessing使用的是序列化pickle来在多进程之间转移数据,而socket对象是不能被序列化的,但是在linux操作系统上却没问题,因为在linux上多进程multiprocessing使用的是fork,所以在windows上可以改用多线程。因为网络通信属于io密集型的操作,对cpu计算要求不高,不用多进程,用多线程就行。

     

    参考资料:

    https://stackoverflow.com/questions/33064982/how-can-i-shred-socket-object-over-python-multiprocess-it-raises-eoferror

    https://github.com/dpkp/kafka-python/issues/46

    https://docs.python.org/2/library/multiprocessing.html#windows

  • 相关阅读:
    深入理解计算机系统 第六章 存储器层次结构 第二遍
    深入理解计算机系统 第六章 存储器层次结构
    深入理解计算机系统 第八章 异常控制流 Part2 第二遍
    深入理解计算机系统 第八章 异常控制流 part2
    深入理解计算机系统 第八章 异常控制流 Part1 第二遍
    深入理解计算机系统 第八章 异常控制流 part1
    深入理解计算机系统 第三章 程序的机器级表示 Part2 第二遍
    深入理解计算机系统 第三章 程序的机器级表示 part2
    深入理解计算机系统 第三章 程序的机器级表示 Part1 第二遍
    深入理解计算机系统 第三章 程序的机器级表示 part1
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/11927235.html
Copyright © 2011-2022 走看看