celery / jumpstarter

MIT License
7 stars 3 forks source link

Implement the ThreadedContextManager for synchronous resources #13

Closed thedrow closed 3 years ago

thedrow commented 3 years ago

Since synchronous resources are by definition synchronous, we need to acquire & release them using a thread pool. Therefore, we must wrap them in a ThreadedContextManager which schedules __enter__ and __exit__ in the thread pool.

The ThreadedContextManager follows the classic Adapter design pattern.

By default, we use a global CapacityLimiter passed into the anyio.run_sync_in_worker_thread(). The capacity limiter is set to the current number of CPUs.

You may optionally pass another CapacityLimiter instance.