Don't understand this asyncio error
Here's the code.
import asyncio
import aiohttp
async def fetch_numbers(session):
counter = 0
while True:
counter += 1
resp = await session.post("https://httpbin.org/post", json={"number": counter})
yield await resp.json()
async def main():
async with aiohttp.ClientSession() as session:
counter = 0
async for item in fetch_numbers(session):
counter += 1
print(item)
if counter == 99:
break
if __name__ == '__main__':
asyncio.run(main())
Here's the error I get:
unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
I feel like this error shouldn't happen? I'm not sending anything using the generator. I'm open to any suggestions!
Posted Apr 10, 2019, 1:27 PM UTC
From my reading here, it looks like the issue might be the fetch_numbers iterator. It looks like you might need to call next() on it or to call send(None) to it before you try to iterate over it.
A iterating through something calls next() though, right?
Yeah. But if I'm reading that PEP right, this is a peculiarity of that particular kind of coroutine generator.