3_no_locks.py 489 Bytes
#!/usr/bin/env python
import asyncio
import random

finished_count = 0

async def slow_op(n):
    global finished_count
    print("slow_op {} started".format(n))
    await asyncio.sleep(random.uniform(0.0, 1.0))
    finished_count += 1
    print("slow_op {} complete".format(n))

async def main():
    await asyncio.wait([
        slow_op(i) for i in range(10)
        ])

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print("finished_count = {}".format(finished_count))