Celery thread pool: multithreading within a Celery worker
Codemia Knowledge Hub ·
To run tasks in threads inside a Celery worker, start it with --pool=threads. Celery mainly uses multiprocessing or event-driven pools (gevent or Eventlet), but the thread pool is a good fit for I/O-bound or high-latency tasks that are not CPU intensive, where processes would add memory overhead without much benefit.
Workers, pools and concurrency
Workers are the processes that execute tasks. A Celery system can have several workers, and each worker can run multiple tasks at once, depending on its concurrency settings.
Switching the worker to threads
Set the --pool option to threads (or solo for single-threaded mode) when you start the worker:
With threads, each worker is still a single process, but it manages multiple threads internally to execute tasks.
When threads help
- Memory efficiency: threads share the memory space of their parent process, so they generally use less memory than processes.
- I/O-bound tasks: for tasks that mostly wait on I/O (network responses, disk reads and writes), threads let many of them run concurrently.
Where threads hurt
- The GIL: because of Python's Global Interpreter Lock, threads may not truly run in parallel, especially for CPU-bound tasks. This matters much less for I/O-bound tasks.
- Thread safety: race conditions, deadlocks and shared state across threads need careful handling.
Example: an I/O-bound task on the thread pool
- Define the Celery application:
- Run the worker with the thread pool:
Practices for the thread pool
- Use threads for I/O-bound tasks: tasks that mostly wait on I/O benefit most.
- Limit the thread count: depending on workload and hardware, too many threads degrade performance.
- Handle errors and log: handle exceptions inside tasks and log properly so threading issues can be tracked down.
Thread pool at a glance
| Aspect | Description |
| Pool Type | threads for multithreading |
| Best Use Case | I/O-bound tasks (network, disk operations) |
| Command to Start Worker | celery -A proj worker --pool=threads |
| GIL Impact | Significant in CPU-bound tasks, manageable in I/O-bound tasks |
| Memory Usage | Lower than multiprocessing |
| Concurrency | Must manage thread safety |
Threads are not Celery's default mode because of the GIL, but for I/O-bound work they use memory and resources efficiently, as long as your tasks are thread safe.
Related reading
- My kafka docker container cannot connect to my zookeeper docker container
- Mystery about Kafka's retention period
- Need to understand kafka broker property log.flush.interval.messages
- Need to use Apache Kafka in my React-Native app
- MySQL Get character-set of database or table or column?
- n-largest elements in a sequence need to retain duplicates
- Must access to scala.collection.immutable.List and Vector be synchronized?
- Must call EndRead in ALL cases?
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.