Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top