What is Threading?

Threading allows to make multiple threads of execution to take place in a Python program. Though threads appear to run simultaneously, only one can be executed at a time (enforced by Python’s global interpreter lock). Threading is a helpful module when working with tasks that are I/O bound (including web-oriented tasks like web scraping or downloading files).

Here’s an example of Threading:

import threading
import time


def eat_breakfast():
    time.sleep(3)
    print("You eat breakfast")


def drink_coffee():
    time.sleep(4)
    print("You drank coffee")


def study():
    time.sleep(5)
    print("You finish studying")


x = threading.Thread(target=eat_breakfast, args=())
x.start()

y = threading.Thread(target=drink_coffee, args=())
y.start()

z = threading.Thread(target=study, args=())
z.start()

x.join()
y.join()
z.join()

print(threading.active_count())
print(threading.enumerate())
print(time.perf_counter())

The method active_count() returns the number of thread objects that are currently there. The returned number is equal to the length of the list returned by the method ennumerate().

Sources: Threading – Python Python Multithreading

Author: Zende_

From PA. EHS 2025. Does computer programming and such. That's really it.

Leave a Reply

Your email address will not be published. Required fields are marked *