import asyncio
class Timer:
stop = False
async def start(self):
ii = 0
while not self.stop:
ii += 1
await asyncio.sleep(.001)
return ii
async def random_stop(timer):
await asyncio.sleep(4)
timer.stop = True
return 'finish'
async def _main():
timer = Timer()
r = await asyncio.gather(timer.start(), random_stop(timer))
print(r)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(_main())
finally:
loop.close()