How Do Threads Work in Python, and What Are Common Python-Threading Specific Pitfalls

How do threads work in Python, and what are common Python-threading specific pitfalls?

Yes, because of the Global Interpreter Lock (GIL) there can only run one thread at a time. Here are some links with some insights about this:

  • http://www.artima.com/weblogs/viewpost.jsp?thread=214235
  • http://smoothspan.wordpress.com/2007/09/14/guido-is-right-to-leave-the-gil-in-python-not-for-multicore-but-for-utility-computing/

From the last link an interesting quote:

Let me explain what all that means.
Threads run inside the same virtual
machine, and hence run on the same
physical machine. Processes can run
on the same physical machine or in
another physical machine. If you
architect your application around
threads, you’ve done nothing to access
multiple machines. So, you can scale
to as many cores are on the single
machine (which will be quite a few
over time), but to really reach web
scales, you’ll need to solve the
multiple machine problem anyway.

If you want to use multi core, pyprocessing defines an process based API to do real parallelization. The PEP also includes some interesting benchmarks.

Do I use a whole processor thread when I start a thread in Python?

No, you are not using a full thread don't worry about that.

You could check this SO topic for more info: How do threads work in Python, and what are common Python-threading specific pitfalls?

Another library that might interest you is asyncio: https://docs.python.org/3/library/asyncio.html

Threading in python, error when creating threads with specific string input

TemporalWolf is right, it takes a tuple. If you just put brackets round a string, it is just taken to be mathematical brackets. If you add a comma, python interprets it as a tuple:

threading.Thread(name="Something", target=self.bot_creater, args=("bot_1",)).start()

What are some common pitfalls developers run into with multi-threading?

The classic difficulty with multithreaded applications is two different threads modifying the same memory at the same time. The method of solving this problem is called synchronization.

See my more complete answer here.



Related Topics



Leave a reply



Submit