Forums

Parallel http requests from single http request

What's the recommended way to make parallel http requests (outbound to slow APIs I use) from a single inbound http request?

The general form of this question is how to do multiple async coroutines in parallel, e.g. using asyncio.create_task or asyncio.to_thread (will these work?).

If it matters I have a paid account and am using Flask.

Hmm, that would be tricky from website code on PythonAnywhere, at least with our primary web-hosting system. We do have an experimental system for ASGI support that would allow you to use async HTTP in website code, but right now it's command-line only and not quite production grade. Do you think it's something you could potentially offload to an always-on task via a task queue? Here's a help page with some hints and tips on doing that.

Per https://flask.palletsprojects.com/en/stable/async-await/ this should be supported with WSGI while continuing to use 1 worker for the lifetime of the inbound http request: "Each request still ties up one worker, even for async views. The upside is that you can run async code within a view, for example to make multiple concurrent database queries, HTTP requests to an external API, etc."

I'm looking for exactly the "multiple concurrent" ability for outbound requests that it mentions. Maybe that'll just work if I try deploying? It's not really a fit for a task queue coz it's still in the context and lifetime of the inbound http request.

I recommend experimenting with a simple example. Here's how to set up an ASGI web app if you'd like to try the experimental feature: https://help.pythonanywhere.com/pages/ASGICommandLine/