简体   繁体   English

来自PEP-492的协同程序是否绕过Python 3.5中的GIL?

[英]Do coroutines from PEP-492 bypass the GIL in Python 3.5?

Python 3.5 includes co-routine support with PEP-492 ; Python 3.5包括与PEP-492的协同常规支持; which is great and all.... assuming the coroutines go around the GIL ; 这是伟大的,所有....假设协同程序围绕GIL ; does anyone know if that's the case? 有谁知道是否是这种情况? Or, should I just keep using the multiprocessing module? 或者,我应该继续使用multiprocessing模块吗?

I would say, that coroutines do not bypass the GIL. 我会说,协同程序不会绕过GIL。

The reason is, that coroutines are never processed in parallel. 原因是协同程序从不并行处理。 Coroutines are a language feature that does some kind of pseudo-parallel processing without real parallel task, thread or anything else execution. 协同程序是一种语言功能,它可以执行某种伪并行处理,而无需真正的并行任务,线程或其他任何执行。 Only one coroutine is ever executed at once. 一次只执行一个协程。

Remember: Even, when using coroutines, you can still have different threads in your program! 记住:即使在使用协同程序时,程序中仍然可以有不同的线程!

So, the GIL is not affected, because the GIL is only a means to prevent real parallel processing of threads in specific parts of the Python interpreter, that could end in corruption of global data. 因此,GIL不会受到影响,因为GIL只是一种阻止Python解释器特定部分中线程真正并行处理的方法,可能会导致全局数据损坏。

When you are using a thread-enabled version of Python, you will have the GIL -- and no thread and also no coroutines "bypass" the GIL. 当你使用支持线程的Python版本时,你将获得GIL - 没有线程也没有协同程序“绕过”GIL。 But the coroutines are not affected by the GIL, as threads are, since threads could be stopped by the GIL, when entering critical sections. 但协同程序不受GIL的影响,因为线程可以在进入关键部分时由GIL阻止线程。 Coroutines are not, unless a second thread is running ... (but that is the problem of the threading in your program, not of the coroutines). 协同程序不是,除非第二个线程正在运行...(但这是程序中的线程问题,而不是协同程序的问题)。

Of course, you can (at least it was possible some time ago) create a Python interpreter version with no thread-support (when you really don't need it), by compiling the interpreter yourself. 当然,通过自己编译解释器,你可以(至少在不久前可能)创建一个没有线程支持的Python解释器版本(当你真的不需要它时)。 In such a version, the GIL should be not executed. 在这样的版本中,不应该执行GIL。

But you must be sure, that no module you are using, is using threading, since that module would break. 但是你必须确定,你正在使用的模块没有使用线程,因为该模块会破坏。

Edit: After reading your question a second time, I guess, what you really want to ask is, if the GIL-overhead (applicable in threads) is lower in coroutines. 编辑:在第二次阅读你的问题后,我想,你真正想要问的是,如果协程中的GIL开销(适用于线程)较低。

I would say, yes. 我会说,是的。 Even when the GIL is active in your version of the interpreter. 即使GIL在您的解释器版本中处于活动状态。 Because by limiting your execution to cooperative multiprocessing, the GIL will not (or less, when you still have more than one thread) affect your coroutines, as it is when you have multiple worker threads. 因为通过将执行限制为协作多处理,GIL不会(或更少,当你仍有多个线程时)影响你的协程,就像你有多个工作线程一样。 There will be less (or no) contention on reserving the GIL. 保留GIL的争用将会减少(或没有)。

There is also the webserver "Tornado" that uses coprocessing technologies for a longer time now in Python, very successfully. 还有网络服务器“Tornado”,它在Python中使用协同处理技术已经很长时间了,非常成功。 That should show, that coprocessing is definitively a good choice, when using Python. 这应该表明,当使用Python时,协同处理绝对是一个不错的选择。 Also there are other examples of programs that are fast by using coprocessing technology (eg Nginx). 还有其他通过使用协处理技术(例如Nginx)快速的程序示例。

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

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