Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What exactly is a thread?

I have done some other python tutorials, and read articles, and keep seeing the concept of 'threads' come up, and the explanations are always pretty technical. Anyone care to put into plain english, as it seems like a pretty important concept.

Thanks!

great answers, and thank you. so Chris Freeman, Steven Parker, Mario Blokland, how does one apply threading? I imagine the answer could get large and complicated fast, but just a general idea would suffice...so is it like writing your code a certain way? writing functions, or is just something naturally done in coding without really thinking in terms of 'threading' ? thanks again!

Steven Parker
Steven Parker
229,644 Points

Coding for threads takes special attention to avoid situations that might produce errors and potentially crash the program.

Consider that the clones (threads) may be doing the same job, but each needs it's own desk and tools. If they try to use the same resources and supplies, they might interfere with each other. The unique "desk" each thread has is known as its context. Code that manage the separate contexts without interference is called "thread-safe".

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The actual coding of threading is beyond what can be put in a post. Googling can find tutorials on-line. Maybe it will be included in a Treehouse course someday. Deciding on using threading is easier to discuss.

The decision comes down to: Can you tolerate only doing one thing at a time? A good candidate for threading is when you have something that may take a long time and you don't want to block the rest of your code from continuing. As one example, say you have a website admin panel that let's you email all of your subscribers. There isn't necessarily any output from this process you would need to see, but you don't want to wait for it to complete. You might want a separate thread to run that will cycle through the emailing process so you can continue with other work. The Python [celery](www.celeryproject.org/) module was created to help with these type of tasks.

As Steven mentioned, a lot of thought needs to go into architecting multi-threaded code to avoid the pit-falls of race-conditions, starvation, dead-lock, live-lock, data sharing (and not sharing). Googling "multithreading pitfalls" gives lots of examples, like this blog post: http://austingwalters.com/multithreading-common-pitfalls/

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

By default, programs start as single-threaded, that is, they have a single thread or path of execution. In a single thread, only one thing can be done at a time. If something takes a long time (like file I/O), then all other execution is blocked until that thing completes. By adding threading to a program, each thread (or flow) operates independently and they do not block each other. This allows other things to be be done while a long, slow task is running in another thread.

Terms you may hear: "fork" is to start another independent process with all the attributes of the current process. "join" is to reconnect the flow of two forked processes back into a single flow or thread. Each process is running in its own thread.

Imagine that you are a boss of a company. If you have no employees yet, you have to get your tasks done alone (you = thread one).

You may have to write a document and talk to clients. You cannot do all those things at the same time and lets say you need 8 hours for all these things, alone. If you decide to employ someone (employee = second thread), then maybe he could write the documents while you talk on the phone with your clients AT THE SAME TIME. So instead of 8 hours you now only need 4 hours to get all those things done.

This is what threading does, it splits work to multiple threads which finish tasks independently.

Hope it helps.

Steven Parker
Steven Parker
229,644 Points

Important, yes, yet tricky to define simply. But I'll give it a shot.

Threads are involved with multi-tasking, which is getting a computer to do more than one thing at the same time (or what appears to be the same time). Imagine that a program makes a "clone" of itself to do one job while another part is doing a different job. A program can "clone" itself more times if it needs to do more things at once. The job each "clone" is doing is called a task, and the "clone" itself is called a thread.

Does that help clarify it?

The thread is a piece of code which get something done.