Skip to content

how to improve hashlib speed with async #7911

Answered by dmontagu
scheung38 asked this question in Questions
Discussion options

You must be logged in to vote

You can use starlette's run_in_threadpool function to run any expensive functions that release the GIL. It's not clear exactly what you are doing but if the problem is just that hashing so many large files is taking a long time, using run_in_threadpool might speed it up -- file IO will release the GIL, and I think the hashing functions might as well (not sure), so you should be able to achieve similar performance to what you'd get through multiprocess-based parallelism.

But it will depend a lot on what you are doing. If what you are asking about isn't specific to FastAPI this question may be better asked somewhere else like stack overflow.

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by tiangolo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
3 participants
Converted from issue

This discussion was converted from issue #871 on February 28, 2023 11:13.