Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.88k stars 77 forks source link

How to extend Black Sheep to multiple threads #289

Closed WilliamSoCreat closed 1 year ago

WilliamSoCreat commented 1 year ago
Note: consider using Discussions to open a conversation about new features…

you know tornado can do it though it will lead thundering herd(惊群效应,i dont know if it translate correct .or The herd effect 🚀 Feature Request

RobertoPrevato commented 1 year ago

Hi @WilliamSoCreat If you are thinking about using multiple threads to handle request handlers, that is out of the scope of blacksheep now. You should look into how to do it with the underlying ASGI HTTP Server (e.g. uvicorn, hypercorn, etc.).

In the past blacksheep was not ASGI compatible and it implemented its own HTTP server - see the before-asgi tag - files, and it was using multi-processing to support more threads / processes each handling one event loop.

If instead you have in mind using multiple threads to handle CPU intensive operations, this can be done using the features offered by asyncio itself, that are thread pool executors and process executors.

Example: let's imagine you want to resize pictures using Pillow as part of your request handling. In this scenario, you should use multi-threading using a ThreadPool executor to not block the event loop used to handle web requests. You can see an example of this here: Torino /server/domain/pictures.py.

I mentioned this in the blacksheep documentation but didn´t put an example there.

I hope this helps. 😄

WilliamSoCreat commented 1 year ago

uvicorn, hypercorn is what i want thread pool executors and process executors is what i want in the future thank you so much 😄