Python和真正的并发线程 [英] Python and truly concurrent threads

查看:152
本文介绍了Python和真正的并发线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几个小时,我可以完全弄清楚python多线程比单线程要快的多.

I've been reading for hours now and I can completely figure out how python multi threading is faster than a single thread.

这个问题确实源于GIL.如果有GIL,并且在任何时候实际上只有一个线程正在运行,那么多线程怎么会比单个线程快?

The question really stems from GIL. If there is GIL, and only one thread is really running at any single time, how can multi threading be faster than a single thread?

我了解到,通过一些操作,GIL已释放(例如写入文件).那是什么使多线程更快?

I read that with some operations GIL is released (like writing to file). Is that what makes multi threading faster?

关于小菜.这些对并发有何帮助?到目前为止,我所看到的所有目的都是在函数之间轻松切换,并简化复杂的yield函数.

And about greenlets. How do those help with concurrency at all? So far all the purpose I see for them is easy switching between functions and less complicated yield functions.

在世界上,像Tornado这样的服务器如何处理数千个同时发生的连接?

And how in the world a server like Tornado can deal with thousands of simultaneous connections?

推荐答案

您是正确的-当python等待C代码执行时,GIL被释放,这就是提高速度的方法.但是一次只能执行一行Python.请注意,这是CPython(实现)的详细信息,并非严格地讲是语言python本身的一部分.例如, Jython和IronPython没有GIL,可以完全利用多处理器系统..

You are correct - when python is waiting on C code execution the GIL is released, and that is how you can get some speedup. But only one line of python can be executed at a time. Note that this is a CPython (implementation) detail, and not strictly speaking part of the language python itself. For example, Jython and IronPython have no GIL and can fully exploit multiprocessor systems.

如果您需要使用CPython进行真正的并发编程,则应查看多重处理而不是线程.

If you need truly concurrent programming in CPython, you should be looking at multiprocessing rather than threading.

这篇关于Python和真正的并发线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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