Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and truly concurrent threads

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?

like image 457
Anton Avatar asked Feb 08 '13 03:02

Anton


1 Answers

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.

like image 133
wim Avatar answered Nov 15 '22 08:11

wim