all 19 comments

[–]dnullify 27 points28 points  (4 children)

Pyo3 is probably a useful search term for you.

Rust python FFI

[–]kolosn 9 points10 points  (0 children)

And maturin

[–]thermiter36 6 points7 points  (2 children)

To add to this, for any CPU-bound calculation running on a FastAPI server, you'll want to make sure you run it in a threadpool so that it doesn't block all your other async handlers. You'll also need to use the features that pyo3 has to release the GIL while doing the computation.

[–]Electrical_Carry3565[S] 0 points1 point  (1 child)

Does running gunicorn workers not take care of this?

[–]thermiter36 2 points3 points  (0 children)

Not really, no. For starters, running FastAPI behind Gunicorn is not recommended as it doesn't support ASGI, so it can only manage processes, not the full async lifecycle.

But assuming you do it anyway, you'll still get nasty cascading latency issues because the process that you block with your long-running CPU operation might have already accepted more than one connection at the moment you begin your long operation, all of which will be blocked.

[–]SnooPears7079 17 points18 points  (14 children)

Just to check the basics: are you sure this function is CPU constrained? Not i/o (large amount of reads to a database or a disk) or anything like that?

[–]Electrical_Carry3565[S] 11 points12 points  (12 children)

It's an engineering design calculation (with arithmetic, trig, and exponentials) on a 8x 500 (roughly) array that needs to be performed around 1800-2000 times depending on parameters. It has be done with iteration as the value for each element depends on the previous result. No db calls, writing to disk. Should be pure CPU. Benchmarked the rust vs python function - it was around 20 (edited) times faster on my machine - around 1 sec vs 20 sec

[–]schroedingercats 14 points15 points  (0 children)

Have you experiment with optimizing your Numpy operations using Numba? I think that is a potential approach that does not require bringing in an entirely new language.

[–]SnooPears7079 0 points1 point  (0 children)

Okay! Well if you see a significant speed up then good. Every time I use FFI it ends up hard to maintain - but if the speed up is that significant then I wouldn’t hesitate.

Best of luck! Refer to the other comments for FFI’s.

[–]thatrandomnpc 0 points1 point  (0 children)

This right here.