Python and truly concurrent threads

主宰稳场 提交于 2019-12-01 17:54:27

问题


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

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?

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

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.

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/14765071/python-and-truly-concurrent-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!