为Python程序分配多个核心

时间:2021-03-07 21:03:32

I notice when I run my heavily CPU dependant python programs, it only uses a single core. Is it possible to assign multiple cores to the program when I run it?

我注意到当我运行我依赖CPU的高级python程序时,它只使用一个核心。在运行程序时是否可以为程序分配多个内核?

3 个解决方案

#1


13  

You have to program explicitly for multiple cores. See the Symmetric Multiprocessing options on this page for the many parallel processing solutions in Python. Parallel Python is a good choice if you can't be bothered to compare the options, look at the examples here.

您必须为多个内核明确编程。有关Python中的许多并行处理解决方案,请参阅此页面上的“对称多处理”选项。并行Python是一个不错的选择,如果你不能打扰比较选项,请看这里的例子。

Some problems can't take advantage of multiple cores though. Think about how you could possibly run up the stairs faster with the help of three friends. Not going to happen!

但是有些问题无法利用多核。想想如何在三个朋友的帮助下更快地跑上楼梯。不会发生!

#2


4  

If any part of your problem can be run in parallel, you should look into the multiprocessing module

如果您的问题的任何部分可以并行运行,您应该查看多处理模块

#3


4  

I wonder why nobody mentioned CPython's GIL (Global Interpreter Lock) yet. It basically means that multiple threads inside one Python interpreter cannot use the power of multiple cores because many operations are protected by a global lock in order to be thread-safe. This only applies to a small amount of applications - the CPU-bound ones. For more info, just search for the term "GIL", there are already many questions on it (like that one, for example).

我想知道为什么没人提到CPython的GIL(全球翻译锁)。它基本上意味着一个Python解释器中的多个线程无法使用多个内核的强大功能,因为许多操作都受全局锁保护,以便是线程安全的。这仅适用于少量应用程序 - 受CPU限制的应用程序。有关更多信息,只需搜索术语“GIL”,就已经有很多问题(例如,那个问题)。

This answer of course assumes that you are in fact using multiple threads, or else you won't be able to use multiple cores anyway (multiprocessing would be another possibility).

这个答案当然假设您实际上使用多个线程,否则无论如何您将无法使用多个核心(多处理将是另一种可能性)。

#1


13  

You have to program explicitly for multiple cores. See the Symmetric Multiprocessing options on this page for the many parallel processing solutions in Python. Parallel Python is a good choice if you can't be bothered to compare the options, look at the examples here.

您必须为多个内核明确编程。有关Python中的许多并行处理解决方案,请参阅此页面上的“对称多处理”选项。并行Python是一个不错的选择,如果你不能打扰比较选项,请看这里的例子。

Some problems can't take advantage of multiple cores though. Think about how you could possibly run up the stairs faster with the help of three friends. Not going to happen!

但是有些问题无法利用多核。想想如何在三个朋友的帮助下更快地跑上楼梯。不会发生!

#2


4  

If any part of your problem can be run in parallel, you should look into the multiprocessing module

如果您的问题的任何部分可以并行运行,您应该查看多处理模块

#3


4  

I wonder why nobody mentioned CPython's GIL (Global Interpreter Lock) yet. It basically means that multiple threads inside one Python interpreter cannot use the power of multiple cores because many operations are protected by a global lock in order to be thread-safe. This only applies to a small amount of applications - the CPU-bound ones. For more info, just search for the term "GIL", there are already many questions on it (like that one, for example).

我想知道为什么没人提到CPython的GIL(全球翻译锁)。它基本上意味着一个Python解释器中的多个线程无法使用多个内核的强大功能,因为许多操作都受全局锁保护,以便是线程安全的。这仅适用于少量应用程序 - 受CPU限制的应用程序。有关更多信息,只需搜索术语“GIL”,就已经有很多问题(例如,那个问题)。

This answer of course assumes that you are in fact using multiple threads, or else you won't be able to use multiple cores anyway (multiprocessing would be another possibility).

这个答案当然假设您实际上使用多个线程,否则无论如何您将无法使用多个核心(多处理将是另一种可能性)。