1

I was trying to get asyncio to work in parallel, which I thought was its bread and butter? However, it's executing my calls one after another. What am I missing?

async def fetch(stock):
    ticker = yfinance.Ticker(stock)
    info = ticker.info
    return info

async def get_results():
    task1 = asyncio.create_task(fetch('AAPL'))
    task2 = asyncio.create_task(fetch('TSLA'))
    task3 = asyncio.create_task(fetch('GME'))
    print(await asyncio.gather(task1, task2, task3))
asyncio.run(get_results())
J. Doe
  • 571
  • 1
  • 10
  • 24
  • 4
    Asyncio code never runs in parallel, it can just release control to allow other tasks to execute. And if the code you run is not made for this and will not release control, it will run more or less synchronously. – Klaus D. May 17 '22 at 18:00
  • See https://stackoverflow.com/questions/27435284/multiprocessing-vs-multithreading-vs-asyncio-in-python-3 for some context – Jeff May 17 '22 at 18:10

0 Answers0