https://github.com/pogodigitalism/syncinasync
A Python class for running non-blocking synchronous code in async
https://github.com/pogodigitalism/syncinasync
async concurrency discord-py sync threadpool
Last synced: 5 months ago
JSON representation
A Python class for running non-blocking synchronous code in async
- Host: GitHub
- URL: https://github.com/pogodigitalism/syncinasync
- Owner: PogoDigitalism
- Created: 2023-11-24T15:58:59.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-26T15:55:11.000Z (over 2 years ago)
- Last Synced: 2025-03-12T17:36:36.380Z (about 1 year ago)
- Topics: async, concurrency, discord-py, sync, threadpool
- Language: Python
- Homepage:
- Size: 33.2 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SyncInAsync
## Call synchronous functions in asynchronous coroutines without blocking the event loop! ☑️
Especially useful for asynchronous libraries like FastAPI, Discord.py etc.
## HOW IT WORKS:
1. SyncInAsync creates a ThreadPoolExecutor (or uses a passed one) to execute these synchronous functions in seperate threads. It also takes the running event loop.
2. This execution happens through Async IO's run_in_executor.
3. run_in_executor returns a Future object which is then used to return the Future's result.
4. This result is the result of your synchronous function!
### ⚠️Try to prevent too many threads running concurrently:
*If max_workers is None or not given, it will default to the number of processors on the machine, multiplied by 5, assuming that ThreadPoolExecutor is often used to overlap I/O instead of CPU work and the number of workers should be higher than the number of workers for ProcessPoolExecutor.* [concurrent.futures docs](https://docs.python.org/3/library/concurrent.futures.html)
*You can check your amount in Python with **os.cpu_count()***