当前位置: 首页 > 工具软件 > Nameko > 使用案例 >

python nameko并发_如何并发使用异步模块调用nameko服务

鲜于阳成
2023-12-01

我写了一个这样的异步程序。一个永久运行循环同时启动4事件。每个事件都将运行rpc服务。在nameko服务中,我用time.sleep(10)实现服务。在

我搞不懂为什么服务每隔10秒就完成一次。我想服务应该同时结束。我怎么能让工作同时完成?在def start_loop(loop):

asyncio.set_event_loop(loop)

loop.run_forever()

async def job(x):

try:

with ClusterRpcProxy(CONFIG) as rpc:

res = rpc.helloworldService.helloworld(x)

print(res)

except Exception as e:

print(f"{e}")

async def do_sleep(x, queue):

try:

await job(x)

queue.put("ok")

except Exception as e:

print(f"{e}")

def consumer():

asyncio.run_coroutine_threadsafe(do_sleep('10', queue), new_loop)

asyncio.run_coroutine_threadsafe(do_sleep('11', queue), new_loop)

asyncio.run_coroutine_threadsafe(do_sleep('12', queue), new_loop)

asyncio.run_coroutine_threadsafe(do_sleep('13', queue), new_loop)

if __name__ == '__main__':

print(time.ctime())

new_loop = asyncio.new_event_loop()

loop_thread = Thread(target=start_loop, args=(new_loop,))

loop_thread.setDaemon(True)

loop_thread.start()

CONFIG = {'AMQP_URI': "amqp://guest:guest@localhost"}

queue = Queue()

sema = asyncio.Semaphore(2)

consumer_thread = Thread(target=consumer)

consumer_thread.setDaemon(True)

consumer_thread.start()

while True:

msg = queue.get()

print("current:", time.ctime())

namekorpc服务是:

^{pr2}$

输出如下:hello_10

current: Sat Jan 26 13:04:57 2019

hello_11

current: Sat Jan 26 13:05:07 2019

hello_12

current: Sat Jan 26 13:05:17 2019

hello_13

current: Sat Jan 26 13:05:28 2019

 类似资料: