TypeError: can't pickle _thread.lock objects 分布式进程学习 中出现的错误
QueueManager.register('get_task_queue', callable=get_task()) 原因是这里的callable的参数赋值方法加了() 去掉就行了
# coding:utf-8 # WINDOWS import queue from multiprocessing.managers import BaseManager from multiprocessing import freeze_support # 任务个数 task_number = 10 # 定义收发队列 task_queue = queue.Queue(task_number) result_queue = queue.Queue(task_number) # 创建类似的queuemanager class QueueManager(BaseManager): pass def get_task(): global task_queue return task_queue def get_result(): global result_queue return result_queue def win_run(): # 先定义函数在绑定 QueueManager.register('get_task_queue', callable=get_task()) QueueManager.register('get_result_queue', callable=get_result()) manager = QueueManager(address=('127.0.0.1', 8001), authkey=b'qiye') # 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', 8001), authkey=b'qiye') # 启动 manager.start() # 通过网络获取任务队列和结果队列 task = manager.get_task_queue() result = manager.get_result_queue() try: # 添加任务 for url in ['ImgUrl_'+str(i) for i in range(10)]: print('put task %s...' % url) task.put(url) print('try to get result') for i in range(10): print('result is %s' % result.get(timeout=10)) except queue.Empty: print('result is empty') finally: #关闭 manager.shutdown() if __name__ == '__main__': # windows多进程可能有问题 加以下代码缓解 freeze_support() win_run()