How do I get around the chicken and egg issue here?
One function will return the first result of a generator, this generator must gather data from the function that called it. This works in general code, but as soon as you throw async in the loop (I don't want to return a coroutine) it errors out. How do I not return a coroutine from function_one
?
Code:
import asyncio
async def second_iterator(number):
for x in range(number):
yield await function_one(x)
async def function_one(number):
if number > 2:
return asyncio.run(second_iterator(number))
await asyncio.sleep(1)
return number
def main(number):
print(asyncio.run(function_one(number)))
main(3)
Error:
Traceback (most recent call last):
File "main.py", line 17, in <module>
main(3)
File "main.py", line 15, in main
print(asyncio.run(function_one(number)))
File "C:\Users\Owner\Anaconda3\lib\asyncio\runners.py", line 43, in run
return loop.run_until_complete(main)
File "C:\Users\Owner\Anaconda3\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "main.py", line 9, in function_one
return asyncio.run(second_iterator(number))
File "C:\Users\Owner\Anaconda3\lib\asyncio\runners.py", line 34, in run
"asyncio.run() cannot be called from a running event loop")
RuntimeError: asyncio.run() cannot be called from a running event loop