代码拉取完成,页面将自动刷新
#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...')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。