简体   繁体   English

Python GIL和多线程

[英]Python GIL and multithreading

I would like to separate my sigle-thread application to number of working threads. 我想将我的单线程应用程序与工作线程数分开。 Just 1 question - what about performance of this action? 只是一个问题-该操作的执行情况如何? If GIL prevents python from executing more than 1 thread at the time will I have any profit? 如果GIL阻止python同时执行多个线程,我会从中获利吗?

Another point (from c/c++ point of view) - as I know each thread, anyway, can be only executed exclusively, so in the lower level than python interpreter I have the same limitation. 另一点(从c / c ++的角度来看)-正如我所知道的,无论如何,每个线程只能被独占执行,因此在比python解释器低的级别上,我有相同的限制。

Summary: Will the the python threads have lesser efficiency that 'native' thread in part of task switching? 简介:在任务切换过程中,python线程的效率是否会低于“ native”线程?

Don't worry about the GIL. 不用担心GIL。 Depending on the kinds of things your program does (calculation vs. I/O) you will have different performance characteristics. 根据您程序执行的操作种类(计算与I / O),您将具有不同的性能特征。 If your program is I/O bound then you probably won't notice the GIL at all. 如果您的程序受I / O约束,那么您可能根本不会注意到GIL。

Another approach is to use the multiprocessing module where each process runs in its own OS process with its own Python runtime. 另一种方法是使用多处理模块,其中每个进程都使用自己的Python运行时在其自己的OS进程中运行。 You can take full advantage of multiple cores with this approach, and it's usually safer because you don't have to worry about synchronising access to shared memory. 通过这种方法,您可以充分利用多个内核,并且通常更安全,因为您不必担心同步访问共享内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM