You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Docket provides fine-grained concurrency control that allows you to limit the number of concurrent tasks based on specific argument values. This is essential for protecting shared resources, preventing overwhelming external services, and managing database connections.
383
+
384
+
### Basic Concurrency Limits
385
+
386
+
Use `ConcurrencyLimit` to restrict concurrent execution based on task arguments:
print(f"Customer 1001 has {active_count} active tasks")
536
+
537
+
# List all active concurrency keys
538
+
keys =await redis.keys("docket:concurrency:*")
539
+
for key in keys:
540
+
count =await redis.scard(key)
541
+
print(f"{key}: {count} active tasks")
542
+
```
543
+
544
+
### Best Practices
545
+
546
+
1.**Choose appropriate argument names**: Use arguments that represent the resource you want to protect (database name, customer ID, API endpoint).
547
+
548
+
2.**Set reasonable limits**: Base limits on your system's capacity and external service constraints.
549
+
550
+
3.**Use descriptive scopes**: When you have multiple unrelated concurrency controls, use different scopes to avoid conflicts.
551
+
552
+
4.**Monitor blocked tasks**: Tasks that can't start due to concurrency limits are automatically rescheduled with small delays.
553
+
554
+
5.**Consider cascading effects**: Concurrency limits can create queuing effects - monitor your system to ensure tasks don't back up excessively.
555
+
556
+
Concurrency control helps you build robust systems that respect resource limits while maintaining high throughput for independent operations.
557
+
380
558
These advanced patterns enable building sophisticated distributed systems that can adapt to changing conditions, handle operational requirements, and provide the debugging and testing capabilities needed for production deployments.
0 commit comments