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. Android Studio 设置项目Module编码,解决Android Studio项目执行时乱码问题

    Android Studio的项目设置逻辑与Eclipse有非常大的差别.运行的操作为File->Setting->File Encodings然后来进行设置,如图所看到的: waterm ...

  2. 【Python】输出程序运行的百分比

    对于一些大型的Python程序.我们须要在命令行输出其百分比,显得更加友好,以免被人误会程序陷入死循环.假死的窗口. 关键是利用到不换行的输出符\r,\r的输出.将直接覆盖掉此行的内容. 比方例如以下 ...

  3. linux下的环境文件设置说明

    工作环境设置文件 环境设置文件有两种:系统环境设置文件 和 个人环境设置文件   1.系统中的用户工作环境设置文件:   登录环境设置文件:/etc/profile        非登录环境设置文件: ...

  4. 你必须了解的java内存管理机制(二)-内存分配

    前言 在上一篇文章中,我们花了较大的篇幅去介绍了JVM的运行时数据区,并且重点介绍了栈区的结构及作用,相关内容请猛戳!在本文中,我们将主要介绍对象的创建过程及在堆中的分配方式. 相关链接(注:文章讲解 ...

  5. 关于erlang中的timer:tc/3

    timer:tc/3对于统计函数运行时间是个很不错的函数, 截图timer:tc/1,tc/2,tc/3的API: 拿斐波那契数列入手做个讲解: -module(fib). -export([fib/ ...

  6. Operation not permitted - /usr/bin/pod

    问题描述:执行sudo gem install cocoapods, 提示出错:While executing gem ... (Errno::EPERM)     Operation not per ...

  7. leetcode题目解答报告(2)

    Pascal's Triangle 题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  8. 6 Maven聚合与集成

    Maven的聚合特性能够把项目的各个模块聚合在一起构件,而Maven的继承特性能够帮助抽取各个模块相同的依赖和插件等配置,简化POM的同时,还能促进各个模块配置的一致性.     1.聚合     为 ...

  9. 对于atomic nonatomic assign retain copy strong weak的简单理解

    atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作 1)atomic 设置成员变量的@property属性时,atomic是默认值,提供多线程安全 在多线程环 ...

  10. php遍历统计文件目录和文件

    function total($dirname, &$dirnum, &$filenum){ $dir=opendir($dirname); readdir($dir)."& ...