TypeError: can't pickle _thread.lock objects

Posted ychao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: can't pickle _thread.lock objects相关的知识,希望对你有一定的参考价值。

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()

  

以上是关于TypeError: can't pickle _thread.lock objects的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: can't pickle dict_values objects

TypeError: data argument can't be an iterator

TypeError: can't convert console.log(...) to primitive type

TypeError: can't compare offset-naive and offset-aware datetimes bugfix

python3.4.0 input Can't convert 'int' object to str implicitly 如何正确输出?

TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to