简体   繁体   English

使用 asyncio function 时从 while 循环返回一个值

[英]return a value from while loop when using asyncio function

I am trying to connect and recieve messages from multiple websockets concurrently.我正在尝试同时连接和接收来自多个 websocket 的消息。 For this purpose I made it with asyncio, and it prints messages correctly.为此,我使用 asyncio 制作它,它可以正确打印消息。 But the problem is that I just can print it, not return it.但问题是我只能打印它,不能退货。

The simplified example of pseudo code which I am struggle with is as below:我正在努力的伪代码的简化示例如下:

import websockets
import json

symbols_id = [1,2]

## LOOP RUNNING EXAMPLE OF ASYNCIO
async def get_connect(symbols_id):
    tasks = []
    for _id in symbols_id:
        print('conncetion to', _id)
        if _id == 1:
            a = 0
        elif _id == 2:
            a = 200
        tasks.append(asyncio.create_task(_loop(a)))

    return tasks

async def _loop(a):
    while True:
        print(a)
        a+=1
        await asyncio.sleep(2.5)

async def ping_func():
    while True:
        print('------ ping')
        await asyncio.sleep(5)


async def main():
    tasks = await get_connect(symbols_id)
    asyncio.create_task(ping_func())
    await asyncio.gather(*tasks)

asyncio.run(main())

As you can see from the code above I used print(a) to print a in each loop.从上面的代码可以看出,我使用print(a)在每个循环中打印a I test return a instead of print(a) but it was not helpful.我测试return a而不是print(a)但它没有帮助。

thanks谢谢

yield a ? yield a return a will exit the function and the loop, yield is usually what you want in asyncio for looped tasks return a将退出 function 和循环, yield通常是你在 asyncio 中想要的循环任务

Finally I found the way of using yield and async for to read data in each loop.最后我找到了使用yieldasync for在每个循环中读取数据的方法。 It will work correctly, by changing the code to the following one.通过将代码更改为以下代码,它将正常工作。

import websockets
import json

symbols_id = [1,2]

global a
a=0

## LOOP RUNNING EXAMPLE OF ASYNCIO
async def get_connect(symbols_id):
    tasks = []
    for _id in symbols_id:
        print('conncetion to', _id)
        if _id == 1:
            a = 0
        elif _id == 2:
            a = 200
        tasks.append(asyncio.create_task(_loop(a)))

    return tasks

async def _loop(param):
    global a
    a = param
    while True:
        print(a)
        a+=1
        await asyncio.sleep(2.5)

async def ping_func():
    while True:
        print('------ ping')
        await asyncio.sleep(5)

async def get_result():
    global a
    while True:
        yield a
        await asyncio.sleep(1)

async def main():
    tasks = await get_connect(symbols_id)
    asyncio.create_task(ping_func())
    async for x in get_result():
        print(x)
    await asyncio.gather(*tasks)

asyncio.run(main())

I was confused with how to use generated data from this code snippet inside the other code snippet.我对如何在另一个代码片段中使用此代码片段生成的数据感到困惑。 what I found is:我发现的是:

1- Generated data can be accessible with global variables. 1- 生成的数据可以通过全局变量访问。

2- By defining a class and a property, it can be accessible from every part of the code. 2- 通过定义 class 和属性,可以从代码的每个部分访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM