Python分布式进程报错:pickle模块不能序列化lambda函数
今天在学习到廖老师Python教程的分布式进程时,遇到了一个错误:_pickle.PicklingError: Can't pickle <function <lambda> at 0x000001710FDC2EA0>: attribute lookup <lambda> on __main__ failed(pickle模块不能序列化lambda函数)
代码如下:
#!/usr/bin/env python
# _*_ coding:utf-8 _*_ import random, queue
from multiprocessing.managers import BaseManager # 发送任务的队列
task_queue = queue.Queue()
# 接收结果的队列
result_queue = queue.Queue()
# 从BaseManager继承的QueueManager class QueueManager(BaseManager):
pass # 吧两个Queue都注册到网络上,callable参数关联了Queue对象
QueueManager.register('get_task_queue', callable=lambda: task_queue)
QueueManager.register('get_result_queue', callable=lambda: result_queue) # 绑定端口5000,设置验证码abc
manager = QueueManager(address=('', 5000), authkey=b'abc') # 启动queue
manager.start() # 获得通过网络访问的Queue对象
task = manager.get_task_queue()
result = manager.get_result_queue() # 放几个任务
for i in range(10):
n = random.randint(0, 1000)
print('添加任务 %d' % n)
task.put(n) # 从result队列读取结果
print('尝试获取结果')
for i in range(10):
r = result.get(timeout=10)
print('结果是:%s' % r) # 关闭
manager.shutdown()
print('master exit')
在教程中我记得有关pickle的事儿(有印象,看来思想还在线上,哈哈),翻了一下,看到:
原来是系统问题造成的,那么,如何解决呢?在教程中我也看到,遇到这样的情况,需要我们自己定义函数,实现序列化。
所以对代码稍加修改,定义两个函数return_task_queue和return_result_queue实现序列化:
#!/usr/bin/env python
# _*_ coding:utf-8 _*_ import random, queue
from multiprocessing.managers import BaseManager # 发送任务的队列
task_queue = queue.Queue()
# 接收结果的队列
result_queue = queue.Queue() def return_task_queue():
global task_queue
return task_queue def return_result_queue():
global result_queue
return result_queue # 从BaseManager继承的QueueManager class QueueManager(BaseManager):
pass if __name__ == '__main__':
# 吧两个Queue都注册到网络上,callable参数关联了Queue对象
QueueManager.register('get_task_queue', callable=return_task_queue)
QueueManager.register('get_result_queue', callable=return_result_queue) # 绑定端口5000,设置验证码abc
manager = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc') # 启动queue
manager.start() # 获得通过网络访问的Queue对象
task = manager.get_task_queue()
result = manager.get_result_queue() # 放几个任务
for i in range(10):
n = random.randint(0, 1000)
print('添加任务 %d' % n)
task.put(n) # 从result队列读取结果
print('尝试获取结果')
for i in range(10):
r = result.get(timeout=10)
print('结果是:%s' % r) # 关闭
manager.shutdown()
print('master exit')
运行结果:
欢迎各位来和我一起分享技术,交流学习心得
Python分布式进程报错:pickle模块不能序列化lambda函数的更多相关文章
- [python](windows)分布式进程问题:pickle模块不能序列化lambda函数
运行错误:_pickle.PicklingError: Can't pickle <function <lambda> at 0x000002BAAEF12F28>: attr ...
- python数据持久存储:pickle模块的基本使用
经常遇到在Python程序运行中得到了一些字符串.列表.字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据. 这个时候Pickle模块就派上用场了,它可以将对象转换 ...
- python数据持久存储:pickle模块的使用
python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...
- GoldenGate 复制进程报错"OGG-01296 Error mapping",丢弃文件报错“Mapping problem with delete record (target format)”,且实际条目存在
故障描述: (1).复制进程 Abended,通过view report语句查看可发现类似如下的报错: 2017-10-23 15:01:43 ERROR OGG-01296 Error mappin ...
- 【python】python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte sequence
python读取文件报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 2: illegal multibyte ...
- 【python】python安装tensorflow报错:python No matching distribution found for tensorflow==1.12.0
python安装tensorflow报错:python No matching distribution found for tensorflow==1.12.0 python版本是3.7.2 要安装 ...
- [转]python数据持久存储:pickle模块的基本使用
python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...
- mac下python环境pip报错[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) 的解决方法
1.mac下python环境pip报错: issuserdeMacBook-Pro:~ issuser$ pip install pyinstallerCollecting pyinstaller ...
- Django中修改DATABASES后,执行python manage.py ****报错!UnicodeEncodeError
Django中修改DATABASES后,执行python manage.py ****报错!UnicodeEncodeError: 'latin-1' codec can't encode chara ...
随机推荐
- StackExchange.Redis 之 hash 类型示例
StackExchange.Redis 的组件封装示例网上有很多,自行百度搜索即可. 这里只演示如何使用Hash类型操作数据: // 在 hash 中存入或修改一个值 并设置order_hashkey ...
- learn about sqlserver partitition and partition table 1
Dear all, Let get into business, the partitions on sql server is very different with that on oracle. ...
- 题解【[HNOI2010]弹飞绵羊】
\[ \texttt{Description} \] 有 \(n\) 个弹力装置排成一排,第 \(i\) 个弹力装置的弹力系数是 \(k_i\) ,绵羊到第 \(i\) 个装置时,会被弹到第 \(i+ ...
- Spark API 之 map、mapPartitions、mapValues、flatMap、flatMapValues详解
原文地址:https://blog.csdn.net/helloxiaozhe/article/details/80492933 1.创建一个RDD变量,通过help函数,查看相关函数定义和例子: & ...
- java服务器端线程体会
一个完整的项目包括服务器和客服端 服务器端初步编写: (1) 服务器端应用窗口的编写 (服务器类Server): 包括窗口和组件的一些设置, 添加一些客服端的元素,如客服端在线用户表(Vector), ...
- Linux恢复删除的文件
linux恢复删除的文件 先介绍下一些文件的基本概念: · 文件实际上是一个指向inode的链接, inode链接包含了文件的所有属性, 比如权限和所有者, 数据块地址(文件存储在磁盘 ...
- Angular常用命令
一. Angular常用命令 1. ng new 文件夹名 (新建项目,选择y使用路由) 2. ng serve --open (默认浏览器运行项目) 3. ng serve --port 6060 ...
- Vue 路由&组件懒加载(按需加载)
当打包构建应用时,Javascript 包会变得非常大,影响页面加载速度.使用Vue路由懒加载和组件懒加载可以提升页面加载速度,减少白屏时间,提升用户体验. 用法有如下三种:(路由懒加载与组件懒加载用 ...
- 07.JS对象-2
前言: 学习一门编程语言的基本步骤(01)了解背景知识(02)搭建开发环境(03)语法规范(04)常量和变量(05)数据类型(06)数据类型转换(07)运算符(08)逻辑结构(09)函数(10)对象1 ...
- 【React Native错误集】* What went wrong: Execution failed for task ':app:installDebug'.
错误1:* What went wrong: Execution failed for task ':app:installDebug'. > com.android.builder.testi ...