Could somebody explain me why I can't execute my tasks if I start the loop without any added tasks before? (Python 3.7)
import asyncio
import threading
def run_forever(loop):
loop.run_forever()
async def f(x):
print("%s executed" % x)
# init is called first
def init():
print("init started")
loop = asyncio.new_event_loop()
# loop.create_task(f("a1")) # <--- first commented task
thread = threading.Thread(target=run_forever, args=(loop,))
thread.start()
loop.create_task(f("a2")) # <--- this is not being executed
print("init finished")
If I leave comment on # loop.create_task(f("a1")) the execution is:
init started
init finished
Uncommented execution is:
init started
init finished
a1 executed
a2 executed
Why so? I wanted to create a loop and to add tasks in the future.