• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads in python  RSS feed

Rodrigo Bossini
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here's a snippet from the book Head First Python:

"Threads do indeed exist in Python but should be avoided where possible.
This has nothing to do with the quality of Python’s threading library and
everything to do with Python’s implementation, especially the implementation
known as CPython (which is more than likely the one you’re running
now). Python is implemented using a technology known as the Global
Interpreter Lock (GIL), which enforces a restriction that Python can only
ever run on a single interpreter process, even in the presence of multiple
processors. What all this means to you is that your beautifully designed and implemented
program that uses threads will never run faster on multiple processors even
if they exist, because it can’t use them. Your threaded application will run
serially and, in many cases, run considerably slower than if you had developed
the same functionality without resorting to threads.
Main message: don’t use threads with Python until the GIL restriction is
removed…if it ever is."

Does that mean that usually one uses threading in python only when the application has both IO-Bound and CPU-Bound activities?
Steve Luke
Posts: 4181
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For CPython, yes. But if you are using a different implementation of Python, it may have a different threading system which makes full multi-threaded applications possible. For example Jython uses Java's underlying threading library, and IronPython uses .NET's threading library, making them both implementations that are not limited by the GIL.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!