python 支持多处理器/多核编程吗?
多处理器编程和多核编程有什么区别?
最好用 python 展示如何编写用于多处理器编程的小程序和示例。 多核编程
What is the difference between multiprocessor programming and multicore programming?
Preferably show examples in python how to write a small program for multiprocessor programming & multicore programming
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不存在“多处理器”或“多核”编程之类的东西。 作为应用程序程序员,“多处理器”和“多核”计算机之间的区别可能与您无关。 它与内核如何共享内存访问的微妙之处有关。
为了利用多核(或多处理器)计算机,您需要一个以可以并行运行的方式编写的程序,以及一个允许该程序实际上在多个核上并行执行的运行时(以及操作系统,尽管您可以在 PC 上运行的任何操作系统都可以执行此操作)。 这确实是并行编程,尽管并行编程有不同的方法。 与 Python 相关的是多处理和多线程。
在 C、C++、Java 和 C# 等语言中,您可以通过执行多个线程来编写并行程序。 CPython 和 PyPy 运行时中的全局解释器锁排除了此选项; 但仅限于那些运行时。 (在我个人看来,多线程危险且棘手,而且通常Python 鼓励您不要将其视为获得性能优势的一种方式。)
如果您想编写一个可以在 Python 中的多个内核上运行的并行程序,您有几种不同的选择:
threading
模块和在 IronPython 或 Jython 运行时中运行它。processing
模块(现在作为 < a href="https://docs.python.org/2/library/multiprocessing.html" rel="noreferrer">multiprocessing
模块),在多个进程中运行代码立刻。subprocess
模块运行多个 python 解释器并在它们之间进行通信。无论您选择哪个选项,您都需要了解如何将程序正在执行的工作分成有意义的块。 由于我不确定您正在考虑编写什么样的程序,因此很难提供有用的示例。
There is no such thing as "multiprocessor" or "multicore" programming. The distinction between "multiprocessor" and "multicore" computers is probably not relevant to you as an application programmer; it has to do with subtleties of how the cores share access to memory.
In order to take advantage of a multicore (or multiprocessor) computer, you need a program written in such a way that it can be run in parallel, and a runtime that will allow the program to actually be executed in parallel on multiple cores (and operating system, although any operating system you can run on your PC will do this). This is really parallel programming, although there are different approaches to parallel programming. The ones that are relevant to Python are multiprocessing and multithreading.
In languages like C, C++, Java, and C#, you can write parallel programs by executing multiple threads. The global interpreter lock in the CPython and PyPy runtimes preclude this option; but only for those runtimes. (In my personal opinion, multithreading is dangerous and tricky and it is generally a good thing that Python encourages you not to consider it as a way to get a performance advantage.)
If you want to write a parallel program which can run on multiple cores in Python, you have a few different options:
threading
module and run it in the IronPython or Jython runtime.processing
module, (now included in Python 2.6 as themultiprocessing
module), to run your code in multiple processes at once.subprocess
module to run multiple python interpreters and communicate between them.No matter which of these options you choose, you will need to understand how to split the work that your program is doing up into chunks that make sense to separate. Since I'm not sure what kind of programs you are thinking of writing, it would be difficult to provide a useful example.
正如另一篇文章中提到的,Python 2.6 有 multiprocessing 模块,它可以利用多核/processors(它通过透明地启动多个进程来绕过 GIL)。 它提供了一些类似于线程模块的原语。 您将在文档页面中找到一些(简单的)用法示例。
As mentioned in another post Python 2.6 has the multiprocessing module, which can take advantage of multiple cores/processors (it gets around GIL by starting multiple processes transparently). It offers some primitives similar to the threading module. You'll find some (simple) examples of usage in the documentation pages.
您实际上可以编写使用多个处理器的程序。 由于 GIL 锁,您不能使用线程来执行此操作,但可以使用不同的进程来执行此操作。
要么:
You can actually write programs which will use multiple processors. You cannot do it with threads because of the GIL lock, but you can do it with different process.
Either:
如果我理解正确的话,Python 有一种叫做 GIL(全局解释器锁)的东西,它实际上使得在 Python 中执行多线程时无法利用多核。
例如,请参阅 Guido van Rossum 关于该主题的博客条目。 据我所知,“主流”语言中只有C/C++和Java对多核有有效支持。
If I understand things correctly, Python has something called the GIL (Global Interpreter Lock) that effectively makes it impossible to take advantage of multicores when doing multiple threads in Python.
See eg Guido van Rossum's blog entry on the topic. As far as I know, among the "mainstream" languages only C/C++ and Java have effective support for multicores.
您可以阅读有关 python 中的多线程和一般线程的信息
:Python 中的多线程:
http://www.devshed.com/c/a /Python/Python 中的基本线程/
You can read about multithreading in python, and threading in general
Multithreading in Python:
http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/
主要区别在于组织和分发数据的方式。 多核通常在 cpu 中的不同核心之间具有更高的带宽,并且多处理器需要更多地涉及 cpu 之间的总线。
Python 2.6 已经获得了多进程(进程,如程序运行中的那样)和更多用于多线程编程的同步和通信对象。
The main difference is how you organize and distribute data. Multicore typically has higher bandwidths between the different cores in a cpu, and multiprocessor needs to involve the bus between the cpus more.
Python 2.6 has gotten multiprocess (process, as in program running) and more synchronization and communication objects for multithreaded programming.
如果您没有 Python 2.6(例如,如果您使用 Ubuntu Edgy 或 Intrepid,则没有),您可以使用 Google 代码反向移植 版本的多处理。 它是 PyPI 的一部分,这意味着您可以使用 EasyInstall(它是 Ubuntu 中 python-setuptools 包的一部分)轻松安装它。
If you don't have Python 2.6 (which you don't if you're using Ubuntu Edgy or Intrepid for example), you can use the Google code backported version of multiprocessing. It is part of PyPI, which means you can easily install it using EasyInstall (which is part of the python-setuptools package in Ubuntu).