python 2,socketマルチプロセスのエラーpickle.PicklingError: Can't pickle

6610 ワード

#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\r
" response_headers = "Server:My server\r
" response_body = "hello lijizne" response = response_start_line + response_headers + "\r
" + 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     handle_client_process.start()   File "E:\Python2\lib\multiprocessing\process.py", line 130, in start     self._popen = Popen(self)   File "E:\Python2\lib\multiprocessing\forking.py", line 277, in __init__     dump(process_obj, to_child, HIGHEST_PROTOCOL)   File "E:\Python2\lib\multiprocessing\forking.py", line 199, in dump     ForkingPickler(file, protocol).dump(obj)   File "E:\Python2\lib\pickle.py", line 224, in dump     self.save(obj)   File "E:\Python2\lib\pickle.py", line 331, in save     self.save_reduce(obj=obj, *rv)   File "E:\Python2\lib\pickle.py", line 419, in save_reduce     save(state)   File "E:\Python2\lib\pickle.py", line 286, in save     f(self, obj) # Call unbound method with explicit self   File "E:\Python2\lib\pickle.py", line 649, in save_dict     self._batch_setitems(obj.iteritems())   File "E:\Python2\lib\pickle.py", line 681, in _batch_setitems     save(v)   File "E:\Python2\lib\pickle.py", line 286, in save     f(self, obj) # Call unbound method with explicit self   File "E:\Python2\lib\pickle.py", line 548, in save_tuple     save(element)   File "E:\Python2\lib\pickle.py", line 331, in save     self.save_reduce(obj=obj, *rv)   File "E:\Python2\lib\pickle.py", line 419, in save_reduce     save(state)   File "E:\Python2\lib\pickle.py", line 286, in save     f(self, obj) # Call unbound method with explicit self   File "E:\Python2\lib\pickle.py", line 548, in save_tuple     save(element)   File "E:\Python2\lib\pickle.py", line 286, in save     f(self, obj) # Call unbound method with explicit self   File "E:\Python2\lib\pickle.py", line 649, in save_dict     self._batch_setitems(obj.iteritems())   File "E:\Python2\lib\pickle.py", line 681, in _batch_setitems     save(v)   File "E:\Python2\lib\pickle.py", line 286, in save     f(self, obj) # Call unbound method with explicit self   File "E:\Python2\lib\pickle.py", line 748, in save_global     (obj, module, name)) pickle.PicklingError: Can't pickle : 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