[Python] (Windows) Distributed Process issue: Pickle module cannot serialize lambda functions

Source: Internet
Author: User

Run error : _pickle. Picklingerror:can ' t pickle <function <lambda> at 0x000002baaef12f28>: Attribute lookup <lambda> on __m Ain__ failed

The code is as follows:

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4 Importrandom, time, queue5  fromMultiprocessing.managersImportBasemanager6 7 #queue for sending tasks:8Task_queue =queue. Queue ()9 #queue to receive results:TenResult_queue =queue. Queue () One  A #QueueManager inherited from Basemanager: - classQueueManager (basemanager): -     Pass the  - #register two queues on the network, the callable parameter is associated with the queue object: -Queuemanager.register ('Get_task_queue', callable=Lambda: Task_queue) -Queuemanager.register ('Get_result_queue', callable=Lambda: Result_queue) +  - #bind Port 5000, set the Captcha ' abc ': +Manager = QueueManager (address= ("', authkey=b),'ABC') A  at #Start queue: - Manager.start () -  - #to obtain a queue object accessed over the network: -Task =Manager.get_task_queue () -result =Manager.get_result_queue () in  - #put a few tasks in: to  forIinchRange (10): +n = random.randint (0, 10000) -     Print('Put Task%d ...'%N) the task.put (n) *  $ #To read the result from the result queue:Panax Notoginseng Print('Try Get Results ...') -  forIinchRange (10): theR = Result.get (timeout=10) +     Print('Result:%s'%R) A  the #Close: + Manager.shutdown () - Print('Master exit.')

Error message:

1 Traceback (most recent):2File"task_master.py", line 22,inch<module>3 Manager.start ()4File"E:\Anaconda\Anaconda3\lib\multiprocessing\managers.py", Line 513,inchStart5 Self._process.start ()6File"E:\Anaconda\Anaconda3\lib\multiprocessing\process.py", Line 105,inchStart7Self._popen =Self._popen (self)8File"E:\Anaconda\Anaconda3\lib\multiprocessing\context.py", Line 322,inch_popen9     returnPopen (process_obj)TenFile"E:\Anaconda\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 65,inch __init__ One reduction.dump (process_obj, To_child) AFile"E:\Anaconda\Anaconda3\lib\multiprocessing\reduction.py", line 60,inchDump - Forkingpickler (file, protocol). Dump (obj) -_pickle. Picklingerror:can'T Pickle <function <lambda> at 0x000002baaef12f28>: Attribute lookup <lambda> on __main__ failed
    

Cause of Error:Pickle module cannot serialize lambda, need custom Function

Modify the code as follows:

1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4 Importrandom, time, queue5  fromMultiprocessing.managersImportBasemanager6 7 #queue for sending tasks:8Task_queue =queue. Queue ()9 #queue to receive results:TenResult_queue =queue. Queue () One  A #Custom Function Re_task_queue - defre_task_queue (): -     GlobalTask_queue the     returnTask_queue -  - #Custom Function Re_result_queue - defre_result_queue (): +     GlobalResult_queue -     returnResult_queue +  A #QueueManager inherited from Basemanager: at classQueueManager (basemanager): -     Pass -  - if __name__=='__main__': -  -     #register two queues on the network, the callable parameter is associated with the queue object: inQueuemanager.register ('Get_task_queue', callable=re_task_queue) -Queuemanager.register ('Get_result_queue', callable=re_result_queue) to  +     #bind Port 5000, set the Captcha ' abc ': -Manager = QueueManager (address= ('127.0.0.1', authkey=b),'ABC') the  *     #Start queue: $ Manager.start ()Panax Notoginseng  -     #to obtain a queue object accessed over the network: theTask =Manager.get_task_queue () +result =Manager.get_result_queue () A  the     #put a few tasks in: +      forIinchRange (10): -n = random.randint (0, 10000) $         Print('Put Task%d ...'%N) $ task.put (n) -  -     #To read the result from the result queue: the     Print('Try Get Results ...') -      forIinchRange (10):WuyiR = Result.get (timeout=10) the         Print('Result:%s'%R) -          Wu     #Close: - Manager.shutdown () About     Print('Master exit.')

Operation Result:

C:\Users\Lucky, m\python>496234604774 4301 9120 7183 4915317391385798... Try Get Results ...

[Python] (Windows) Distributed Process issue: Pickle module cannot serialize lambda functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.