Threading

The threading module allows multiple threads of execution to take place in a Python program.

While threads may appear to run simultaneously, only one thread can be executed at a time. This is enforced by Python’s global interpreter lock.

Threading is helpful when working with tasks that are I/O bound. This includes web-oriented tasks like scraping or downloading files.

Syntax

import threading

The threading module must first be imported before thread constants can be created and their methods can be used.

Threading

.is_alive()
Returns True if the thread is still running and False, otherwise.
.join()
Delays the flow of execution of a program until the target thread is completely read.
.local()
Returns a local thread object with data that is specific to that thread.
.run()
Executes any target function belonging to a given thread object that is now active.
.start()
Activates and prompts a thread object to be run.
.Thread()
Returns a thread object that can run a function with zero or more arguments.
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn Python on Codecademy