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 in range(10):
        = random.randint(0,1000)
        print('put task %d ...' % n)
        task.put(n)
    print('try get results...')
 
    for in range(10):
        = 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)
 
= QueueManager(address=(server_addr,9833),authkey=b'abc')
m.connect()
 
task = m.get_task_queue()
result = m.get_result_queue()
 
for in range(10):
    try:
        = task.get(timeout = 10)
        print('run task %d * %d' %(n,n))
        = '%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)的更多相关文章

  1. 第23章 COM和ActiveX(COM可以实现跨进程跨机器的函数调用)

    控件对象既可在EXE中实现,也可在DLL中实现.这种实现对于COM对象的用户来说是透明的.因为COM提供了调度服务(marshaling).COM调度机制能够化进程甚至跨机器的函数调用,这使得16位程 ...

  2. Python: 多进程的分布式进程multiprocessing.managers

    multiprocessing.managers 在Thread和Process中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分 ...

  3. 利用multiprocessing.managers开发跨进程生产者消费者模型

    研究了下multiprocessing.managers,略有收获,随笔一篇: 核心思路是构造一个manager进程,这个进程可以通过unix socket或tcp socket与其它进程通信:因为利 ...

  4. 有空可以对C#尝一下鲜,WCF看上去很诱人(跨进程、跨机器、跨子网,跨企业网乃至跨Internet的分布式服务)

    说道底不还是要借助NGNIX实现,PHP自身呢?C#的WCF可以脱离IIS就可以实现跨进程.跨机器.跨子网,跨企业网乃至跨Internet的分布式服务,宿主可以是IIS,WinForm,WPF, Wi ...

  5. python 进程和线程-进程和线程的比较以及分布式进程

    进程和线程的比较 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017631469467456 我们介绍了多进程和多线程,这是实现多任 ...

  6. python_分布式进程中遇到的问题

    看文档学习分布式进程中遇到了一下问题,文档里面例题是python2.X,我用的python3.x,就出现了一下莫名奇妙的问题,最终版代码先呈上: taskManager.py # coding:utf ...

  7. python分布式进程(windows下)

    分布式进程: 在Thread和Process中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分布到同一台机器的多个CPU上. Py ...

  8. python分布式进程

    分布式进程指的是将Process进程分布到多台机器上,充分利用多态机器的性能完成复杂的任务 分布式进程在python 中依然要用到multiprocessing 模块.multiprocessing模 ...

  9. python多进程,进程池,数据共享,进程通信,分布式进程

    一.操作系统中相关进程的知识   Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊.普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次,因为操作系统自动把当前 ...

随机推荐

  1. GDB调试动态链接库

    http://cyukang.com/2012/06/25/gdb-with-libso.html http://cyukang.com/2011/05/06/valgrind.html

  2. Resolving 'Root Partition Is Filling Up' Issue on Sophos UTM Firewall

    from: https://wandersick.blogspot.com/2016/06/resolving-root-partition-is-filling-up.html This is a ...

  3. [Algorithm] Breadth First JavaScript Search Algorithm for Graphs

    Breadth first search is a graph search algorithm that starts at one node and visits neighboring node ...

  4. react 路由传参

    今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...

  5. xhtml三种元素类型

    xhtml三种元素类型:块级元素/内联元素/可变元素 块级元素:独占一行.可自定义自己的宽度和高度.作为其他元素的容器,可容纳其他内联元素和块级元素,喻做一个盒子.内联元素:始终以行内逐个显示.不能设 ...

  6. idea自动注入和自动编译

    ---恢复内容开始--- 自动编译也就是时时编译,当我们写错代码的时候,idea能够马上报错,这个是不错的功能. 安装操作看下图: 下面这个功能的作用就是,如果你不修改的话,你的项目可以正常运行,但是 ...

  7. Django-mysq数据库链接问题

    Django链接MySQL数据库有可能会报no model named MySQLdb, 解决办法: 首先安装pymysql 然后进入到项目目录,找到__init__.py文件,在里面添加 impor ...

  8. spring核心包功能解析

  9. RYU改动监听port Mininet在custom自建拓扑和连接到指定控制器命令解释

    1.RYU控制器改动监听port 在ryu/ryu/ofproto以下的ofproto_common.py watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc ...

  10. jquery动态加载脚本

    如果你使用的是jQuery,它里面有一个内置的方法可以用来加载单个JS文件.当你需要延迟加载一些js插件或其它类型的文件时,可以使用这个方法. 一.jQuery getScript()方法加载java ...