python能够在多个内核上运行吗? [英] is python capable of running on multiple cores?

查看:108
本文介绍了python能够在多个内核上运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:由于python使用"GIL",因此python能够同时运行其单独的线程吗?

Question: Because of python's use of "GIL" is python capable running its separate threads simultaneously?

信息:

阅读对于python是否能够利用多核处理器,我不确定.尽管python做得很好,但认为它缺乏如此强大的功能实在很奇怪.因此感到不确定,我决定在这里询问.如果我编写的是多线程程序,它是否可以在多个内核上同时执行?

After reading this I came away rather uncertain on whether or not python is capable of taking advantage of a multi-core processor. As well done as python is, it feels really weird to think that it would lack such a powerful ability. So feeling uncertain, I decided to ask here. If I write a program that is multi threaded, will it be capable of executing simultaneously on multiple cores?

推荐答案

答案是是,但是..."

The answer is "Yes, But..."

但是当您使用常规线程进行并发操作时,cPython不能.

But cPython cannot when you are using regular threads for concurrency.

您可以使用 multiprocessing mpi4py 将并行工作拆分为另一个进程;

You can either use something like multiprocessing, celery or mpi4py to split the parallel work into another process;

或者您可以使用 Jython IronPython之类的东西使用没有GIL的替代解释器.

Or you can use something like Jython or IronPython to use an alternative interpreter that doesn't have a GIL.

一个较软的解决方案是使用不会在GIL上运行的库来执行繁重的CPU任务,例如 numpy 可以在不保留GIL的情况下完成繁重的工作,因此可以继续使用其他python线程.您还可以通过这种方式使用 ctypes 库.

A softer solution is to use libraries that don't run afoul of the GIL for heavy CPU tasks, for instance numpy can do the heavy lifting while not retaining the GIL, so other python threads can proceed. You can also use the ctypes library in this way.

如果您不做CPU绑定工作,则可以完全忽略GIL问题(有点),因为python在等待IO时不会获取GIL.

If you are not doing CPU bound work, you can ignore the GIL issue entirely (kind of) since python won't aquire the GIL while it's waiting for IO.

这篇关于python能够在多个内核上运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆