Q: Launch four POST requests in parallel I'm starting with multiprocesses and would like to understand a little more. I would like to know if multiprocesses can be applied to my situation and if so how can I do it. I have a situation like the following:
import requests
import json

r1 = requests.post(...).json()
# work on r1
r2 = requests.post(...).json()
# work on r2
r3 = requests.post(...).json()
# work on r3
r4 = requests.post(...).json()
# work on r4

Since each of the four POST requests can take up to 10 seconds, what is the way, if applicable, to launch the four POST requests in parallel?
Thanks for your help.

A: Here's a very basic framework for POSTing in a multithreading environment.
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests

ITERATIONS = 10

def process(url):
    try:
        requests.post(url).raise_for_status()
        return 'OK'
    except (requests.exceptions.HTTPError, Exception) as e:
        return e

def main():
    with ThreadPoolExecutor() as executor:
        for f in as_completed(executor.submit(process, 'https://httpbin.org/post') for _ in range(ITERATIONS)):
            print(f.result())

if __name__ == '__main__':
    main()

