在AsyncIO之间共享数据

时间:2019-05-21 02:09:24

标签: python python-3.x

我试图理解异步和排队,所以我从这个问题中得到了这个样本,以尝试How to exchange values betwwen async function which are running forever in python3.5?

import asyncio
import random

class Demo:
    def __init__(self):
        self.ltp = 0
        self.queue = asyncio.Queue(1)

    async def one(self):
        while True:
            print("Here in one()")
            self.ltp += random.uniform(-1, 1)
            print("one() is sleeping.. value generated = " + str(self.ltp))
            await self.queue.put(self.ltp)

    async def two(self):
        while True:
            print("Here in two()")
            print(self.ltp)
            print("two() is sleeping..")
            await asyncio.sleep(0)

    print("---------------Start----------------")
    loop = asyncio.get_event_loop()
    d = Demo()
    loop.create_task(d.one())
    loop.create_task(d.two())
    loop.run_forever()

当我查看代码输出时,我注意到不再调用one()函数。有没有办法让two()函数还给one(),以便它有机会更新其值?

---------------Start----------------
Here in one()
one() is sleeping.. value generated = 0.0018691287572603077
Here in one()
one() is sleeping.. value generated = 0.9143832681012423
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423
two() is sleeping..
Here in two()
0.9143832681012423

1 个答案:

答案 0 :(得分:0)

我认为以下代码会比您期望的更接近:

import asyncio
import random


class Demo:
    def __init__(self):
        self.ltp = 0
        self.queue = asyncio.Queue(1)

    async def one(self):
        while True:
            print("Here in one()")
            self.ltp += random.uniform(-1, 1)
            print(f"value generated = {str(self.ltp)}")
            await self.queue.put(self.ltp)

    async def two(self):
        while True:
            print("Here in two()")
            val = await self.queue.get()
            print(f"I see {val}")


if __name__ == "__main__":
    d = Demo()
    try:
        print("---------------Start----------------")
        loop = asyncio.get_event_loop()
        asyncio.ensure_future(d.one())
        asyncio.ensure_future(d.two())
        loop.run_forever()
    except Exception:
        pass
    finally:
        print("step: loop.close()")
        loop.close()

这将生成输出:

---------------Start----------------
Here in one()
value generated = -0.6413903994469317
Here in one()
value generated = -1.0685807532617448
Here in two()
I see -0.6413903994469317
Here in two()
Here in one()
value generated = -0.8610724954475495
I see -1.0685807532617448
Here in two()
Here in one()
value generated = -0.7267456040329765
I see -0.8610724954475495
Here in two()
Here in one()
value generated = -1.0264441768086192
I see -0.7267456040329765
Here in two()
Here in one()
value generated = -1.8992963343639357
I see -1.0264441768086192
Here in two()
Here in one()
value generated = -1.2370609104359145
I see -1.8992963343639357
Here in two()
Here in one()
value generated = -0.9568062439608271
I see -1.2370609104359145
Here in two()
Here in one()
value generated = -0.03401334759258323
I see -0.9568062439608271
Here in two()
Here in one()
value generated = 0.5586981511810925
I see -0.03401334759258323
Here in two()

我说这与您的想象更近,因为该顺序与阻塞代码所期望的不完全相同。这是设计使然,还有一个单独的问题。