1. 程式人生 > >分散式程序BaseManager(multiprocessing.managers)

分散式程序BaseManager(multiprocessing.managers)

命令列執行:

#task_master.py

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

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

class QueueManager(BaseManager):
    pass

def task_q():
    return task_queue
def result_q():
    return result_queue


print('master start.')
QueueManager.register('get_task_queue',callable=task_q)
QueueManager.register('get_result_queue',callable=result_q)
manager = QueueManager(address=('127.0.0.1',5000),authkey=b'abc')
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 results...')
for i in range(10):
    r = result.get(timeout=100)
    print('Result: %s' % r)
manager.shutdown()
print('master exit.')


得到:
master start.
master start.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\spawn.py", line 115, in _main
    prepare(preparation_data)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\spawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\spawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 254, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\4視訊教程\PythonExercise Files\task_master.py", line 22, in <module>
    manager.start()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\managers.py", line 479, in start
    self._process.start()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\spawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
在命令列執行:
#task_master.py

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

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

class QueueManager(BaseManager):
    pass

##def task_q():
##    return task_queue
##def result_q():
##    return result_queue

if __name__ == '__main__':
    print('master start.')
    QueueManager.register('get_task_queue',callable= lambda: task_queue)
    QueueManager.register('get_result_queue',callable= lambda: result_queue)
    manager = QueueManager(address=('127.0.0.1',5000),authkey=b'abc')
    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 results...')
    for i in range(10):
        r = result.get(timeout=100)
        print('Result: %s' % r)
    manager.shutdown()
    print('master exit.')

得到:
master start.
Traceback (most recent call last):
  File "task_master.py", line 22, in <module>
    manager.start()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35

-32\lib\multiprocessing\managers.py", line 479, in start
    self._process.start()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35

-32\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35

-32\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Users\lenovo\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\lenovo\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 

0x005656F0>: attribute lookup <lambda> on __main__ failed
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35

-32\lib\multiprocessing\spawn.py", line 100, in spawn_main
    new_handle = steal_handle(parent_pid, pipe_handle)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python35

-32\lib\multiprocessing\reduction.py", line 81, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] 引數錯誤。

但是,以下程式碼可以正常執行:
#task_master.py

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

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

class QueueManager(BaseManager):
    pass

def task_q():
    return task_queue
def result_q():
    return result_queue

if __name__ == '__main__':
    print('master start.')
    QueueManager.register('get_task_queue',callable= task_q)
    QueueManager.register('get_result_queue',callable= result_q)
    manager = QueueManager(address=('127.0.0.1',5000),authkey=b'abc')
    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 results...')
    for i in range(10):
        r = result.get(timeout=100)
        print('Result: %s' % r)
    manager.shutdown()
    print('master exit.')



#task_worker.py

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

class QueueManager(BaseManager):
    pass

QueueManager.register('get_task_queue')
QueueManager.register('get_result_queue')

server_addr = '127.0.0.1'
print('Connect to server %s...' % server_addr)

m = QueueManager(address=(server_addr, 5000), authkey=b'abc')
m.connect()

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

for i in range(10):
    try:
        n = task.get(timeout = 1)
        print('run task %d * %d' % (n, n))
        r = '%d * %d = %d' % (n, n, n*n)
        time.sleep(1)
        result.put(r)
    except Queue.Empty:
        print('task queque is empty')

print('worker exit')