python3 分布式进程(跨机器)BaseManager(multiprocessing.managers)
A机器负责发送任务和接受结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#task_master.py import random,time,queue from multiprocessing.managers import BaseManager task_queue = queue.Queue() result_queue = queue.Queue() class QueueManager(BaseManager): pass 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 = ( '10.10.100.11' , 9833 ),authkey = b 'abc' ) manager.start() task = manager.get_task_queue() result = manager.get_result_queue() for i in range ( 10 ): n = random.randint( 0 , 1000 ) 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.' ) |
B机器负责处理任务和发送结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#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 = '10.10.100.11' print ( 'connect to server %s...' % server_addr) m = QueueManager(address = (server_addr, 9833 ),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 = 10 ) 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 queue is empty' ) print ( 'worker exit' ) |
python3 分布式进程(跨机器)BaseManager(multiprocessing.managers)的更多相关文章
- 第23章 COM和ActiveX(COM可以实现跨进程跨机器的函数调用)
控件对象既可在EXE中实现,也可在DLL中实现.这种实现对于COM对象的用户来说是透明的.因为COM提供了调度服务(marshaling).COM调度机制能够化进程甚至跨机器的函数调用,这使得16位程 ...
- Python: 多进程的分布式进程multiprocessing.managers
multiprocessing.managers 在Thread和Process中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分 ...
- 利用multiprocessing.managers开发跨进程生产者消费者模型
研究了下multiprocessing.managers,略有收获,随笔一篇: 核心思路是构造一个manager进程,这个进程可以通过unix socket或tcp socket与其它进程通信:因为利 ...
- 有空可以对C#尝一下鲜,WCF看上去很诱人(跨进程、跨机器、跨子网,跨企业网乃至跨Internet的分布式服务)
说道底不还是要借助NGNIX实现,PHP自身呢?C#的WCF可以脱离IIS就可以实现跨进程.跨机器.跨子网,跨企业网乃至跨Internet的分布式服务,宿主可以是IIS,WinForm,WPF, Wi ...
- python 进程和线程-进程和线程的比较以及分布式进程
进程和线程的比较 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017631469467456 我们介绍了多进程和多线程,这是实现多任 ...
- python_分布式进程中遇到的问题
看文档学习分布式进程中遇到了一下问题,文档里面例题是python2.X,我用的python3.x,就出现了一下莫名奇妙的问题,最终版代码先呈上: taskManager.py # coding:utf ...
- python分布式进程(windows下)
分布式进程: 在Thread和Process中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分布到同一台机器的多个CPU上. Py ...
- python分布式进程
分布式进程指的是将Process进程分布到多台机器上,充分利用多态机器的性能完成复杂的任务 分布式进程在python 中依然要用到multiprocessing 模块.multiprocessing模 ...
- python多进程,进程池,数据共享,进程通信,分布式进程
一.操作系统中相关进程的知识 Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次,因为操作系统自动把当前 ...
随机推荐
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069495 上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API ...
- Linux以下基于TCP多线程聊天室(server)
接上篇博文,本文是server端的实现,主要实现的功能,就是现实client的连接.转发client发送的消息.以及client掉线提示等功能,同一时候能够在这这上面扩展和TCP以及线程相关的功能木块 ...
- nginx-location rewrite
location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上. 比如, 碰 ...
- viewpager 跳转到指定页面
viewPager.setCurrentItem(getUserIndex(userId)); seCurrentItem是定位到指定页面参数是position
- 关于angular JS 中$timeOut 的一些不正常情况下的$destory
最近项目中存在的问题头疼脑热了好一会. 我先简单说明下问题是由,使用$timeOut循环调用的时候由于页面存在异步加载会出现反复执行循环反复调用$timeOut,怎么清除跳出循环都不管用.于是查到了如 ...
- bootstrap-table自己配置
function initTable(){ var methodNameSearch=$("#methodNameSearch").val(); var requestUrl = ...
- 计算IMEI号的校验位
计算IMEI号的校验位 移动设备国际识别码(IMEI:International Mobile Equipment Identification Number)是差别移动设备的标志,具有唯一性,贴在手 ...
- hdu 3932 Groundhog Build Home
Groundhog Build Home Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- Cocos2d-JS 项目接入 触控广告平台(基于anysdk2.0.2)
本文以Cocos2d-JS项目作为示例,讲解如何集成AnySDK-JS. 一.如何创建项目我就省略了,直接进入主题. 1.1 首先,我需要下载anysdk框架包,下载地址:http://www.any ...
- ElasticSearch(一)什么是全文检索?
全文检索 全文检索,即倒排索引.