import asyncio shutting_down = False async def my_service(): async def _read(): pass while not shutting_down: try: m = await asyncio.wait_for(_read(), 0) except: pass if __name__ == "__main__": loop = asyncio.get_event_loop() loop.create_task(my_service()) loop.run_forever()