Run Class methods in threads python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, a thread can run any callable, including an instance method, a class method, or a static method. The important detail is that you pass the bound method itself as the thread target and then manage shared state carefully, because the thread machinery is easy while the concurrency bugs are not.
Running an Instance Method in a Thread
An instance method is the most common case. If worker.run is the method you want, pass it directly as the target.
Because worker.run is already bound to the instance, Python automatically supplies self. You only pass the remaining method arguments through args.
Running a @classmethod
Class methods work the same way, except the method is bound to the class instead of an instance.
Here, Python supplies cls automatically because Reporter.emit is a bound class method.
Running Several Methods Concurrently
If you want to fan out multiple workers, keep track of the thread objects and join them all:
This pattern is common for I/O-bound tasks such as network requests, file reads, or waiting on external services.
Shared State Needs Protection
Starting threads is easy. Sharing mutable data safely is harder. If multiple threads update the same attribute, use a lock.
Without the lock, the final count can be lower than expected because increments are not automatically atomic at the application level.
Threads Are Best for I/O-Bound Work
Python threads are most useful when the task spends time waiting: HTTP calls, sockets, subprocesses, or disk I/O. For CPU-bound work, threads often do not speed things up much because of the GIL in standard CPython.
If the workload is CPU-heavy, consider:
- '
multiprocessingfor parallel CPU execution' - '
concurrent.futures.ThreadPoolExecutorfor cleaner thread orchestration' - '
asynciowhen the workload is asynchronous I/O rather than threaded blocking calls'
The method can still belong to a class in all of these cases; the scheduling model is what changes.
Common Pitfalls
The most common mistake is calling the method instead of passing it. This is wrong:
That executes run() immediately on the main thread and passes its return value as the target. The correct form is target=worker.run.
Another issue is forgetting that instance methods share the instance. If several threads mutate self at the same time, race conditions are possible unless you synchronize access.
Finally, do not expect threads to accelerate CPU-bound Python loops automatically. They are excellent for I/O-bound concurrency, but a different execution model is usually better for true parallel computation.
Summary
- A thread target can be an instance method, class method, or static method.
- Pass the method itself as
target, not the result of calling it. - Use
argsfor the method parameters that remain afterselforcls. - Protect shared mutable state with locks or another synchronization strategy.
- Prefer threads for I/O-bound work, not for heavy CPU-bound loops in CPython.

