Message344798
Let's start with correct code:
import asyncio
async def writer():
await asyncio.sleep(1)
g1.set_result(41)
async def reader():
await g1
async def test():
global g1
g1 = asyncio.Future()
await asyncio.gather(reader(), writer())
asyncio.run(test())
No error, as expected.
Now let's mess it up a bit:
import asyncio
g1 = asyncio.Future()
async def writer():
await asyncio.sleep(1)
g1.set_result(41)
async def reader():
await g1
async def test():
await asyncio.gather(reader(), writer())
asyncio.run(test())
Fails with RuntimeError ... attached to a different loop
The error makes sense, although it's sad that I can't create global futures / there was no even loop when Future was creates, it was not a *different* event loop / maybe I wish .run() didn't force a new event loop?
A nit (IMO), but I can live with it.
Let's mess the code up a bit more:
import asyncio
g1 = asyncio.Future()
async def writer():
await asyncio.sleep(1)
g1.set_result(41)
async def reader():
await g1
async def test():
await asyncio.gather(reader(), reader(), writer())
asyncio.run(test())
RuntimeError: await wasn't used with future
What?
That's really confusing!
The only difference is that there are now 2 readers running in parallel.
The actual exception comes from asyncio.Future.__await__ after a yield.
I'm not sure how to fix this... |
|
Date |
User |
Action |
Args |
2019-06-06 10:29:59 | Dima.Tisnek | set | recipients:
+ Dima.Tisnek, asvetlov, yselivanov |
2019-06-06 10:29:59 | Dima.Tisnek | set | messageid: <1559816999.36.0.898434753785.issue37172@roundup.psfhosted.org> |
2019-06-06 10:29:59 | Dima.Tisnek | link | issue37172 messages |
2019-06-06 10:29:59 | Dima.Tisnek | create | |
|