python是否支持多处理器/多核编程? [英] Does python support multiprocessor/multicore programming?

查看:489
本文介绍了python是否支持多处理器/多核编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多处理器编程和多核编程之间有什么区别? 最好在python中显示示例,说明如何编写用于多程序化和小型化的小程序.多核编程

What is the difference between multiprocessor programming and multicore programming? preferably show examples in python how to write a small program for multiprogramming & multicore programming

推荐答案

没有多处理器"或多核"编程之类的东西.作为应用程序程序员,多处理器"计算机和多核"计算机之间的区别可能与您无关.这与内核如何共享对内存的访问的微妙之处有关.

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.

为了利用多核(或多处理器)计算机,您需要以一种可以并行运行的方式编写程序,并需要一个运行时,该程序才能在多个计算机上并行执行核心(和操作系统,尽管您可以在PC上运行的任何操作系统都可以执行此操作).这实际上是 parallel 编程,尽管并行编程有不同的方法.与Python相关的是多处理和多线程.

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.

在C,C ++,Java和C#等语言中,您可以通过执行多个线程来编写并行程序. CPython和PyPy运行时中的全局解释器锁排除了此选项;但仅适用于那些运行时. (我个人认为,多线程处理危险且棘手,通常Python鼓励您不要将它视为获得性能优势的一种方式.)

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.)

如果要编写一个可以在Python的多个内核上运行的并行程序,则有几种不同的选择:

If you want to write a parallel program which can run on multiple cores in Python, you have a few different options:

  • Write a multithreaded program using the threading module and run it in the IronPython or Jython runtime.
  • Use the processing module, (now included in Python 2.6 as the multiprocessing module), to run your code in multiple processes at once.
  • Use the subprocess module to run multiple python interpreters and communicate between them.
  • Use Twisted and Ampoule. This has the advantage of not just running your code across different processes, but (if you don't share access to things like files) potentially across different computers as well.

无论您选择这些选项中的哪一个,都将需要了解如何将程序正在完成的工作拆分为有意义的块以进行分离.由于我不确定您打算编写哪种程序,因此很难提供有用的示例.

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是否支持多处理器/多核编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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