简体   繁体   中英

Why is my asynchronous version of my python script not faster than the synchronous version?

I'm just starting experimenting with the asyncio library in python. My purpose is to speed up my code, however with my first script there really is no improvement than doing it without asyncio.

from yahoo_fin import stock_info as si
from yahoo_fin.stock_info import *
import time
import asyncio

async def price(stock):

    prijs = str(si.get_live_price(stock))
    await asyncio.sleep(0.001)
    print(prijs)


def main():
    loop = asyncio.get_event_loop()
    t0 = time.time()
    task =  asyncio.gather(
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('adbe'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('adbe')

    )
    loop.run_until_complete(task)

    t1 = time.time()
    print("took %.2f ms" % (1000*(t1-t0)))

if __name__ == '__main__':
    main()

If I compare it without coding it asynchronous:

from yahoo_fin import stock_info as si
from yahoo_fin.stock_info import *
import time
import asyncio

def price(stock):
    prijs = str(si.get_live_price(stock))
    print(prijs)


def main():

        t0 = time.time()

        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('adbe'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('aapl'),
        price('fcx'),
        price('acn'),
        price('adbe')



        t1 = time.time()
        print("took %.2f ms" % (1000*(t1-t0)))


if __name__ == '__main__':
    main()

I would think that the asynchronous version runs all the price() calls at the same time thus reducing the time to execute the program? Am i doing something wrong?

Thanks

You haven't done anything wrong here. However your understanding on async in python is flawed.

Python can't do two things at once due to the GIL. It is impossible. You can fake doing multiple things with python threads and async but it isn't true multitasking. If you want true multitasking you would need to use the multiprocessing module in python but that's out of the scope of this question.

So basically in async when you await a function you tell python hey this call is doing something that will take time before it returns, however that waiting means that python isn't calculating anything it literally is waiting for something, for instance doing IO bound tasks not CPU tasks. You're program here is 100% CPU bound since you're not awaiting for the disk to find something or sending/receiving data over a.network using an async function.

Your program is saying this. I will call si.get_live_price(stock) a non async function and wait (not await) the result. After that I will async sleep for 0.001 so someone else can work but after 0.001 no one else can work cause I want control again so I can finish by printing to the console. Once its printed python will now focus on the next task which will repeat the above steps.

This is a topic that can take some time to fully understand but my advice is to look up the difference between concurrency and parallelism in python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM