1 Star 0 Fork 0

liarchoc/learngit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TaskMaker.py 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
573win 提交于 2017-08-16 12:40 . fix connect
#task_master.py
import random,time,queue,threading
from multiprocessing.managers import BaseManager
from multiprocessing import freeze_support#>>>>2.modify:add this if your system is windows
from TaskWorker import calcu#task_worker.py
#send mession queue
task_queue=queue.Queue()
#receive mession queue
result_queue=queue.Queue()
#heir form Base...
class QueueManager(BaseManager):
pass
#>>>>1.modify:replace lamda expression with function def task...
def task_q():
return task_queue
def result_q():
return result_queue
def test():
#register two queue to network and connect para callable to object Queue
QueueManager.register('get_task_queue',callable=task_q)
QueueManager.register('get_result_queue',callable=result_q)
#binding port:8081 and set password 'abc'
manager=QueueManager(address=('192.168.2.171',5000),authkey=b'abc')
#launch queue
manager.start()
time.sleep(2)
#get object queue from network
task=manager.get_task_queue()
result=manager.get_result_queue()
#set mession
for i in range(10):
n=random.randint(0,10000)
print('Put task %d...'%n)
task.put(n)
#get result from queue
print('Try to get results...')
for i in range(10):
r=result.get(timeout=10)
print('Result:%s'%r)
#close
manager.shutdown()
print('master exit.')
if __name__=='__main__':
freeze_support()#>>>>2.modify:add this if your system is windows
t1=threading.Thread(target=test,name='master')
t2=threading.Thread(target=calcu,name='worker')
t1.start()
t2.start()
t1.join()
t2.join()
print('Done...')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/casterfir/learngit.git
[email protected]:casterfir/learngit.git
casterfir
learngit
learngit
master

搜索帮助