Using locks with threading in Python

I was working on a simple multithreaded application in Python where I needed to spawn lots of threads, and each one would write some output to stdout when done working.  Multithreading was viable in my situation because each thread is doing something IO-bound so being limited to a single CPU by the global interpreter lock in Python wasn’t a problem.

I noticed that in my first few runs, I was getting a lot of “smearing” in the output, as though many of the output lines were jumbled together.  After further digging, it turns out print is not thread-safe, so you need to create a lock object and force each thread to acquire it before writing to stdout.  Easy enough.

It’s really cool that python features a robust multithreading library (and multiprocessing as well) where you can create multithreaded apps and tools with like an extra ~20ish lines of code.  Plug it into a Queue object and you can farm threads out to a queue to magically keep the number of concurrent threads to some amount.  Batteries included indeed.  =D

TODO:  share the actual source code, eg. a multithreading snippet.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s