🌍 Python Networking & Multithreading
Estimated reading: 4 minutes 105 views

🧡 Python Multithreading – Create, Sync, and Communicate Threads

🧲 Introduction – Why Use Multithreading in Python?

Python is known for its simplicity and readability, but did you know it also supports multithreading?

Multithreading is a powerful way to:

  • Run multiple tasks concurrently
  • Optimize I/O-bound operations
  • Build responsive applications (e.g., GUI, network services)

Even with Python’s Global Interpreter Lock (GIL), multithreading is ideal for I/O-bound tasks like file I/O, network calls, or database operations.

🎯 In this guide, you’ll learn:

  • Thread lifecycle in Python
  • Creating, starting, joining threads
  • Main thread, daemon threads, thread priorities
  • Thread pools for scalable parallelism
  • Synchronization, deadlocks, and inter-thread communication

πŸ” Thread Lifecycle in Python

A Python thread goes through several states:

StateDescription
NewThread is created but not started yet
RunnableThread is ready to run (started)
RunningActively executing instructions
BlockedWaiting for I/O or a lock
TerminatedExecution completed or stopped

🧠 Python threads are managed by the OS scheduler; Python doesn’t expose all states explicitly.


πŸ› οΈ Create / Start / Join Threads

βœ… Creating a Thread with threading.Thread

import threading

def task():
    print("Thread is running")

t = threading.Thread(target=task)
t.start()   # Starts the thread
t.join()    # Waits for the thread to finish

πŸ’‘ Use join() to synchronize the main thread with the child thread.


βœ… Thread with Arguments

def greet(name):
    print(f"Hello {name}")

t = threading.Thread(target=greet, args=("Alice",))
t.start()
t.join()

βœ… Using Subclassing

class MyThread(threading.Thread):
    def run(self):
        print("Thread via subclass")

t = MyThread()
t.start()
t.join()

🧡 Thread Pools with concurrent.futures

Managing many threads manually is hard. Thread pools offer a higher-level abstraction.

from concurrent.futures import ThreadPoolExecutor

def square(n):
    return n * n

with ThreadPoolExecutor(max_workers=3) as executor:
    results = executor.map(square, [1, 2, 3, 4])
    print(list(results))

βœ… Output:

[1, 4, 9, 16]

πŸ’‘ Ideal for batch processing, parallel tasks, and web scraping.


🧭 Main Thread / Daemon / Priority

βœ… Main Thread

import threading

print("Main thread:", threading.current_thread().name)

🧠 Python automatically starts a main thread when your program begins.


πŸ”₯ Daemon Threads

Daemon threads run in the background and automatically terminate when the main program exits.

def background_task():
    while True:
        print("Running in background")

t = threading.Thread(target=background_task, daemon=True)
t.start()

πŸ’‘ Daemons are great for logging, heartbeat monitors, and cleanups.


⏫ Thread Priority

Python’s threading module does not support priority control directly. Thread scheduling is left to the OS.

πŸ“Œ If you need more control, consider multiprocessing or external libraries like psutil.


πŸ” Synchronization and Deadlocks

Without synchronization, race conditions can occur when multiple threads modify shared data.

βœ… Using threading.Lock

lock = threading.Lock()
counter = 0

def safe_increment():
    global counter
    for _ in range(100000):
        with lock:
            counter += 1

⚠️ Deadlock Example

lock1 = threading.Lock()
lock2 = threading.Lock()

def thread1():
    with lock1:
        with lock2:
            print("Thread1 acquired both locks")

def thread2():
    with lock2:
        with lock1:
            print("Thread2 acquired both locks")

⚠️ These two threads could deadlock by waiting for each other’s lock.

πŸ’‘ Avoid nested locks or use threading.RLock() when needed.


πŸ“¬ Inter-thread Communication

Python threads share memory. Common communication tools include:

βœ… Shared Variables with Lock

queue = []

def producer():
    with lock:
        queue.append("data")

def consumer():
    with lock:
        if queue:
            print(queue.pop(0))

βœ… queue.Queue for Thread-safe Messaging

import queue

q = queue.Queue()

def producer():
    q.put("message")

def consumer():
    msg = q.get()
    print("Received:", msg)

threading.Thread(target=producer).start()
threading.Thread(target=consumer).start()

βœ… queue.Queue is thread-safe and handles blocking automatically.


πŸ“˜ Best Practices for Python Multithreading

βœ… Do This❌ Avoid This
Use threading.Lock() for shared dataModifying shared variables without a lock
Use ThreadPoolExecutor for parallel tasksCreating hundreds of threads manually
Use join() to wait for thread completionIgnoring thread synchronization
Use daemon=True only for background tasksRelying on daemons for important work

πŸ“Œ Summary – Recap & Next Steps

Python multithreading lets you build concurrent, responsive, and efficient applicationsβ€”especially for I/O-bound tasks.

πŸ” Key Takeaways:

  • βœ… Use threading.Thread or ThreadPoolExecutor for concurrent tasks
  • βœ… Understand the thread lifecycle: create β†’ start β†’ run β†’ terminate
  • βœ… Use locks and queues for synchronization and communication
  • βœ… Daemon threads die with the main program; regular threads don’t
  • ⚠️ Deadlocks can occurβ€”always design thread-safe logic

βš™οΈ Real-World Relevance:
Used in web scrapers, network services, chat apps, logging systems, and asynchronous I/O operations.


❓ FAQ – Python Multithreading

❓ Can Python use multiple threads for CPU-bound tasks?

❌ No. Due to the GIL, use multiprocessing for CPU-bound operations.

❓ What is the difference between threading and multiprocessing?

  • threading = shared memory, ideal for I/O-bound tasks
  • multiprocessing = separate memory, ideal for CPU-bound tasks

❓ What is a daemon thread?

βœ… A background thread that automatically terminates when the main program exits.

❓ Can threads share data?

βœ… Yes. Threads share memory, but access must be synchronized.

❓ What is the safest way to communicate between threads?

βœ… Use queue.Queue – it’s thread-safe and handles locking internally.


Share Now :
Share

Python Multithreading

Or Copy Link

CONTENTS
Scroll to Top