Question
How to terminate a function which is working in python?
I want to make a code which works like this: after certain seconds I want to terminate the working function in the main function.
I've tried the below code but when the code in the loop function isn't an async function(like await asyncio.sleep(1)) it doesn't work.
import asyncio
async def loop():
while True:
pass
async def main():
task = asyncio.Task(loop())
await asyncio.sleep(3)
print('1223')
task.cancel()
print('x')
asyncio.run(main())
3 91
3