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. Solaris shell脚本学习

    看懂脚本文件即可 知识点: Shell概述 Shell变量 Shell中的特殊字符 参数置换变量 控制结构 何为shell Shell就是unix提供给用户的使用界面,处于内核和应用程序之间,他是一个 ...

  2. 串匹配算法讲解 -----BF、KMP算法

      参考文章: http://www.matrix67.com/blog/archives/115     KMP算法详解 http://blog.csdn.net/yaochunnian/artic ...

  3. C# HTTP请求后对gzip页面实现解压缩

    1.通过socket页面请求后的receive内容不能经过string后再进行解压缩处理 会造成错误的gzip幻数报错 推荐使用流处理 2.正确分析返回内容 分割header和页面代码部分 3.对页面 ...

  4. Introducing Gradle (Ep 2, Android Studio)

    https://www.youtube.com/watch?v=cD7NPxuuXYY    Introducing Gradle (Ep 2, Android Studio) https://www ...

  5. App性能优化浅谈

    前言 前段时间给公司的小伙伴们进行了关于app性能优化的技术分享.这里我稍微整理一下也给大家分享一下.关于性能优化这个话题非常大,涉及面能够非常广,也能够非常深入.本人能力有限,不会给大家讲特别难懂, ...

  6. java transient关键字(转载)

    博客来源:http://www.blogjava.net/fhtdy2004/archive/2009/06/20/286112.html Volatile修饰的成员变量在每次被线程访问时,都强迫从主 ...

  7. 2014年辛星解读css第五节

    本小节我们解说css中的"盒模型".即"box model",它通经常使用于在布局的时候使用,这个"盒模型"也有人成为"框模型&q ...

  8. session自己定义存储,怎样更好地进行session共享;读tomcat7源代码,org.apache.catalina.session.FileStore可知

    session自己定义存储.怎样更好地进行session共享: 读tomcat源代码,org.apache.catalina.session.FileStore可知 一.详见: 方法1 public ...

  9. (t,p,o) t:p>=o there cannot be more consumer instances in a consumer group than partitions

    https://kafka.apache.org/intro.html Kafka as a Messaging System How does Kafka's notion of streams c ...

  10. Splits a tensor into sub tensors

    https://www.tensorflow.org/api_docs/python/tf/split # 'value' is a tensor with shape [5, 30] # Split ...