Mailing List Archive

Async options for flask app
Hi all,

I'm writing a fediverse server app, similar to kbin https://kbin.pub/en
and lemmy https://join-lemmy.org/. It's like reddit except anyone can
run a server in the same way email works. This architecture involves a
lot of inter-server communication, potentially involving thousands of
different servers.

So, lots of network I/O. Lots of tasks running in parallel to do I/O
with different servers simultaneously. A fair bit of DB access too.

The current fashion is to do this with cooperative multitasking
(async/await/gevent/etc) to avoid the overhead associated with
continually context switching threads and processes. Correct me if I'm
wrong.

How can I do this with Flask? Any async/await tricks? Can I just
configure gunicorn to use gevent worker threads?
https://flask.palletsprojects.com/en/2.3.x/deploying/gunicorn/

Has anyone tried Quart? https://pypi.org/project/quart/

How well does gevent monkey-patch into a Flask app?

A penny for your thoughts

Thanks!

R
--
https://mail.python.org/mailman/listinfo/python-list
Re: Async options for flask app [ In reply to ]
On Mon, 18 Sept 2023 at 13:45, Rimu Atkinson via Python-list
<python-list@python.org> wrote:
>
> Hi all,
>
> I'm writing a fediverse server app, similar to kbin https://kbin.pub/en
> and lemmy https://join-lemmy.org/. It's like reddit except anyone can
> run a server in the same way email works.

And the part you didn't say: It's like Reddit but without a ridiculous
API fee. Right? :)

> The current fashion is to do this with cooperative multitasking
> (async/await/gevent/etc) to avoid the overhead associated with
> continually context switching threads and processes. Correct me if I'm
> wrong.

I wouldn't say "current fashion", but yes, there is significant
benefit in putting more than one task onto any given thread. (Most
likely, if you want maximum throughput, you'll end up wanting a
two-level multiplexer, with a pool of processes, each one managing a
pool of tasks.)

> How can I do this with Flask? Any async/await tricks? Can I just
> configure gunicorn to use gevent worker threads?
> https://flask.palletsprojects.com/en/2.3.x/deploying/gunicorn/

It's been a while since I did it, but it definitely does work, yes.

> Has anyone tried Quart? https://pypi.org/project/quart/

Not me, so I'll let someone else answer that.

> How well does gevent monkey-patch into a Flask app?
>

Here's one where I've done that: https://github.com/rosuav/mustardmine

However, it's worth noting that (a) the exact details might differ
depending on precisely what you use - this is backed by PostgreSQL and
also uses websockets; and (b) I originally did this quite a while ago,
and things may have gotten easier since then. All I know is, it works
if I do it this way, and I haven't felt the urge to mess with that
part of it. You may notice that my README mentions issues with Python
3.8 from back when that was new :)

But with all of those caveats: Yes, this absolutely does work, and I
make use of the Mustard Mine on a regular basis. It gets updates
occasionally, not often, but I bump it onto newer Pythons every now
and then, so the techniques it uses should still be valid.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list