对于3.5以下的Python版本:
import asyncio@asyncio.coroutinedef periodic(): while True: print('periodic') yield from asyncio.sleep(1)def stop(): task.cancel()loop = asyncio.get_event_loop()loop.call_later(5, stop)task = loop.create_task(periodic())try: loop.run_until_complete(task)except asyncio.CancelledError: pass对于Python 3.5及更高版本:
import asyncioasync def periodic(): while True: print('periodic') await asyncio.sleep(1)def stop(): task.cancel()loop = asyncio.get_event_loop()loop.call_later(5, stop)task = loop.create_task(periodic())try: loop.run_until_complete(task)except asyncio.CancelledError: pass


