Python线程出奇地慢

编程入门 行业动态 更新时间:2024-10-10 14:26:32
本文介绍了Python线程出奇地慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我决定学习如何在Python中完成多线程,并且进行了比较以查看在双核CPU上可以获得什么样的性能提升.我发现我的简单多线程代码实际上比顺序运行的慢,并且我不知道为什么.

I have decided to learn how multi-threading is done in Python, and I did a comparison to see what kind of performance gain I would get on a dual-core CPU. I found that my simple multi-threaded code actually runs slower than the sequential equivalent, and I cant figure out why.

我进行的测试是生成大量随机数,然后打印出最大数量的

The test I contrived was to generate a large list of random numbers and then print the maximum

from random import random import threading def ox(): print max([random() for x in xrange(20000000)])

ox()大约需要6秒才能完成我的Intel Core 2 Duo,而ox();ox()大约需要12秒.

ox() takes about 6 seconds to complete on my Intel Core 2 Duo, while ox();ox() takes about 12 seconds.

然后我尝试从两个线程调用ox()来查看完成的速度.

I then tried calling ox() from two threads to see how fast that would complete.

def go(): r = threading.Thread(target=ox) r.start() ox()

go()大约需要18秒才能完成,两个结果在彼此之间的1秒钟之内打印出来.为什么要慢一些?

go() takes about 18 seconds to complete, with the two results printing within 1 second of eachother. Why should this be slower?

我怀疑ox()是自动并行化的,因为如果我查看Windows任务管理器的性能"选项卡,并在我的python控制台中调用ox(),则两个处理器的利用率都会提高到75%左右,直到完成为止. Python会自动并行化max()之类的东西吗?

I suspect ox() is being parallelized automatically, because I if look at the Windows task manager performance tab, and call ox() in my python console, both processors jump to about 75% utilization until it completes. Does Python automatically parallelize things like max() when it can?

推荐答案

  • Python具有GIL. Python字节码一次只能由一个处理器执行.只有某些C模块(不管理Python状态)才能同时运行.
  • Python GIL在锁定线程之间的状态方面有巨大的开销.在较新的版本或开发分支中已对此进行了修复-至少应使多线程CPU绑定代码与单线程代码一样快.
  • 您需要使用多进程框架与Python并行化.幸运的是,Python随附的 multiprocessing 模块使这一操作变得相当容易.

    You need to use a multi-process framework to parallelize with Python. Luckily, the multiprocessing module which ships with Python makes that fairly easy.

    很少有语言可以自动并行化表达式.如果这是您想要的功能,我建议使用Haskell(数据并行Haskell)

    Very few languages can auto-parallelize expressions. If that is the functionality you want, I suggest Haskell (Data Parallel Haskell)

    更多推荐

    Python线程出奇地慢

    本文发布于:2023-11-23 12:56:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1621525.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:线程   出奇   Python

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!