简体   繁体   English

为什么我的 python 脚本的异步版本不比同步版本快?

[英]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.我刚刚开始尝试使用 python 中的 asyncio 库。我的目的是加快我的代码速度,但是对于我的第一个脚本,与不使用 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?我会认为异步版本同时运行所有 price() 调用从而减少执行程序的时间? Am i doing something wrong?难道我做错了什么?

Thanks谢谢

You haven't done anything wrong here.你在这里没有做错任何事。 However your understanding on async in python is flawed.但是,您对 python 中异步的理解存在缺陷。

Python can't do two things at once due to the GIL.由于 GIL,Python不能同时做两件事。 It is impossible.是不可能的。 You can fake doing multiple things with python threads and async but it isn't true multitasking.您可以假装使用 python 线程和异步执行多项操作,但这不是真正的多任务处理。 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.如果你想要真正的多任务处理,你需要使用 python 中的multiprocessing模块,但这不在这个问题的 scope 中。

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.所以基本上是异步的,当你等待 function 时,你告诉 python 嘿,这个调用正在做一些事情,在它返回之前需要一些时间,但是等待意味着 python 没有计算任何它实际上正在等待的东西,例如做 IO 绑定任务不是 CPU 任务。 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.你在这里的程序是 100% CPU 绑定的,因为你不是在等待磁盘找到一些东西或使用异步 function 通过 a.network 发送/接收数据。

Your program is saying this.你的程序是这样说的。 I will call si.get_live_price(stock) a non async function and wait (not await) the result.我将si.get_live_price(stock)称为非异步 function 并等待(不是等待)结果。 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.之后我将异步睡眠 0.001,以便其他人可以工作,但在 0.001 之后没有其他人可以工作,因为我想要再次控制,所以我可以通过打印到控制台来完成。 Once its printed python will now focus on the next task which will repeat the above steps.一旦打印出 python,现在将专注于下一个任务,该任务将重复上述步骤。

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.这是一个可能需要一些时间才能完全理解的主题,但我的建议是在 python 中查找并发和并行之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我教授的LU分解版本比我的快? Python numpy - Why is my prof's version of LU decomposition faster than mine? Python numpy 我的python程序执行速度比同一程序的java版本快。是什么赋予了? - My python program executes faster than my java version of the same program. What gives? 为什么我的 Python 代码比我的 C 代码快? - Why is my Python code faster than my C code? 为什么utorrents Magnet对Torrent文件的获取比我的python脚本更快? - Why is utorrents Magnet to Torrent file fetching is faster than my python script? 为什么我的python版本hoare分区快排序比lomuto分区慢? - Why my python version hoare partition quick sort is slower than lomuto partition? 为什么我的 C++ 程序比 Python 版本慢 100 倍? - Why my C++ program is 100x slower than a Python Version? 为什么我的 function 在 IDLE 中比 python 的 print function 快? - Why is my function faster than python's print function in IDLE? 为什么我的 Python NumPy 代码比 C++ 快? - Why is my Python NumPy code faster than C++? 为什么我的程序比使用内置函数的python快? - Why is my program faster than the one using a python built in function? 为什么我在 C# 中的计算比 Python 快得多 - Why is my computation so much faster in C# than Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM