human
I am working on the file "04-concurrency/sharing_a_variable.ipynb".
The first task is:
3. Using 'asyncio'
In this case, the coroutines yields their control to the "event loop" (also called, the "coordinator") with a await command, which search for the next ready coroutine to be run in a round-robin list. Notice that for this reason, the wall time of this version is smaller than in the previous case.
Can you write Python code for it?
assistant
#Here is the Python code you requested.

import asyncio
import time

a = ''

async def task_1():
    global a
    for i in range(20):
        print('o', end='', flush=True)
        a += 'o'
        await asyncio.sleep(0.5)
        
async def task_2():
    global a
    for i in range(20):
        print('O', end='', flush=True)
        a += 'O'
        await asyncio.sleep(0.25)

now = time.perf_counter()
await asyncio.gather(task_1(), task_2())
elapsed = time.perf_counter() - now
print(f"\nelapsed {elapsed:0.2f} seconds.")
print("a =", a)