class dongtai (): def get_queue ( self ): return Queue () args = locals () lis = [ 'a' , 'b' , 'c' ] for x in lis : args [ "args_{}" . format ( x )] = dongtai () print ( args [ "args_a" ]. get_queue ()) #也可以 #getattr() 应用实例 #
def get_queue(self):
return Queue()
args = locals()
lis = ['a', 'b', 'c']
for x in lis:
args["args_{}".format(x)] = dongtai()
print(args["args_a"].get_queue())
#也可以
#getattr()
应用实例
# encoding:utf-8from multiprocessing.managers import BaseManager
def start_worker(host, port, authkey):
# 由于这个BaseManager只从网络上获取queue,所以注册时只提供名字
for i in range(2):
server_name = "get_task_queue{}".format(i)
BaseManager.register(server_name)
server_name = "put_task_queue{}".format(i)
BaseManager.register(server_name)
# BaseManager.register('get_result_queue')
print ('Connect to server %s' % host)
# 注意,端口port和验证码authkey必须和manager服务器设置的完全一致
worker = BaseManager(address=(host, port), authkey=authkey.encode('utf-8'))
# 链接到manager服务器
worker.connect()
return worker
def get_queue(worker):
get_queue_list=[]
put_queue_list=[]
for i in range(2):
one=getattr(worker,"get_task_queue{}".format(i))()
get_queue_list.append(one)
one = getattr(worker, "put_task_queue{}".format(i))()
put_queue_list.append(one)
# queue_list = sorted(get_queue_list, key=lambda x: x.qsize())
# 选择最优的服务器可以排序 queue_list
# 通道最小数据量也就是延迟最低
# 推荐默认
get_best_qeue = get_queue_list[0]
put_best_qeue = put_queue_list[0]
while True:
# 发送消息接收消息即可
# 自己放里自己取
# 这里要定义一些字典这样 这样子服务就知道怎么处理这些数据
get_best_qeue.put({"add":1})
res=put_best_qeue.get()
print(res)
if __name__ == "__main__":
host = '127.0.0.1'
port = 5000
authkey = 'abc'
# 启动worker
worker = start_worker(host, port, authkey)
# 获取队列
get_queue(worker)