本文介绍了异步两个循环用于不同的 I/O 任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python3 Asyncio 模块来创建负载平衡应用程序.我有两个繁重的 IO 任务:

I am using Python3 Asyncio module to create a load balancing application. I have two heavy IO tasks:

  • 一个 SNMP 轮询模块,用于确定可能的最佳服务器
  • 一个类似代理"的模块,用于平衡对选定服务器的请求.

两个进程都将永远运行,彼此独立,不应被另一个进程阻塞.

Both processes are going to run forever, are independent from eachother and should not be blocked by the other one.

我不能使用 1 个事件循环,因为它们会互相阻塞,有什么办法可以有 2 个事件循环还是我必须使用多线程/处理?

I cant use 1 event loop because they would block eachother, is there any way to have 2 event loops or do I have to use multithreading/processing?

我尝试使用 asyncio.new_event_loop() 但还没有成功.

I tried using asyncio.new_event_loop() but havent managed to make it work.

推荐答案

asyncio 的全部意义在于您可以同时运行数以千计的 I/O 密集型任务,因此您不必完全需要 Threads,这正是 asyncio 的用途.只需在同一个循环中运行两个协程(SNMP 和代理)就可以了.在调用 loop.run_forever() 之前,您必须使它们都可用于事件循环.像这样:

The whole point of asyncio is that you can run multiple thousands of I/O-heavy tasks concurrently, so you don't need Threads at all, this is exactly what asyncio is made for. Just run the two coroutines (SNMP and proxy) in the same loop and that's it. You have to make both of them available to the event loop BEFORE calling loop.run_forever(). Something like this:

import asyncio

async def snmp():
    print("Doing the snmp thing")
    await asyncio.sleep(1)

async def proxy():
    print("Doing the proxy thing")
    await asyncio.sleep(2)

async def main():
    while True:
        await snmp()
        await proxy()

loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()

我不知道你的代码的结构,所以不同的模块可能有自己的无限循环或其他东西,在这种情况下你可以运行这样的东西:

I don't know the structure of your code, so the different modules might have their own infinite loop or something, in this case you can run something like this:

import asyncio

async def snmp():
    while True:
        print("Doing the snmp thing")
        await asyncio.sleep(1)

async def proxy():
    while True:
        print("Doing the proxy thing")
        await asyncio.sleep(2)

loop = asyncio.get_event_loop()
loop.create_task(snmp())
loop.create_task(proxy())
loop.run_forever()

请记住,snmpproxy 都需要以异步感知方式编写的协程(async def).asyncio 不会让简单的阻塞 Python 函数突然异步".

Remember, both snmp and proxy needs to be coroutines (async def) written in an asyncio-aware manner. asyncio will not make simple blocking Python functions suddenly "async".

在您的具体情况下,我怀疑您有点困惑(无意冒犯!),因为编写良好的异步模块永远不会在同一个循环中相互阻塞.如果是这种情况,您根本不需要 asyncio,只需在单独的 Thread 中运行其中之一,而无需处理任何 asyncio> 东西.

In your specific case, I suspect that you are confused a little bit (no offense!), because well-written async modules will never block each other in the same loop. If this is the case, you don't need asyncio at all and just simply run one of them in a separate Thread without dealing with any asyncio stuff.

这篇关于异步两个循环用于不同的 I/O 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 21:50