0

I wrote the code below:

import random, time, queue
from multiprocessing.managers import BaseManager

task_queue = queue.Queue()
result_queue = queue.Queue()

class QueueManager(BaseManager):
    pass

QueueManager.register('get_task_queue', callable=lambda: task_queue)
QueueManager.register('get_result_queue', callable=lambda: result_queue)

manager = QueueManager(address=('', 5000), authkey=b'abd')

manager.start()

task = manager.get_task_queue()
result = manager.get_result_queue()

for i in range(10):
    n = random.randint(0, 10000)
    print('Put task %d...' % n)
    task.put(n)

print('Try get result...')
for i in range(10):
    r = result.get(timeout=10)
    print('Result: %s' % r)

manager.shutdown()
print('master exit.')

but when it runs, I receive this error:

Traceback (most recent call last):
  File "D:/PycharmProjects/test/task_master.py", line 23, in <module>
    manager.start()
  File "C:\Users\tang_ke\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\managers.py", line 479, in start
    self._process.start()
  File "C:\Users\tang_ke\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Users\tang_ke\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Users\tang_ke\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\tang_ke\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\reduction.py", line 59, in dump
    ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function <lambda> at 0x03A67C48>: attribute lookup <lambda> on __main__ failed

Process finished with exit code 1
Cœur
  • 37,241
  • 25
  • 195
  • 267
tangke
  • 19
  • 1
  • 4
  • Please format your code using the editing tools provided. State exactly what you want the code to do, and what it does, and what you have tried. Have you done any debugging at all? –  Jan 17 '17 at 02:35
  • Welcome to Stackoverflow! To get the most out of the site it is important to ask good questions. A guide to asking questions is at: http://stackoverflow.com/help/how-to-ask – Stephen Rauch Jan 17 '17 at 03:20
  • The queues for results and tasks are not connected in any way. If I run your code, the result.get() results in a timeout. The pickling error seems rather strange, I can't reproduce it. – lhk Jan 17 '17 at 09:22
  • Possible duplicate of [Python: Can't pickle type X, attribute lookup failed](https://stackoverflow.com/questions/4677012/python-cant-pickle-type-x-attribute-lookup-failed) – tripleee Jun 26 '17 at 05:59
  • See also http://bugs.python.org/issue19272 which amounts to "lambdas cannot be pickled". – tripleee Jun 26 '17 at 06:01

1 Answers1

2

I got answer in the site:讨论 - 廖雪峰的官方网站

step 1: don't use "lambda" in "QueueManager.register",you have to replace a function,example:

def return_task_queue():
    global task_queue
    return task_queue

QueueManager.register('get_task_queue', callable=return_task_queue)

step 2: you have to add IPAddress when you create "QueueManager",example:

QueueManager(address=('127.0.0.1', 5000), authkey=b'abc')

step 3: you have to put all functions about queuemanager and task and result in a function...example:

def test():
    QueueManager.register('get_task_queue', callable=return_task_queue)
    QueueManager.register('get_result_queue', callable=return_result_queue)
    manager = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc')
    manager.start()
    ....

and you have to use the function "test" in MAIN function,example:

if __name__  == '__main__':
    test()