How to make many async HTTP requests using prepared-requests in Python

Viewed 226

I have a very simple function that, given a link and a few parameters, goes to a website using the requests module but, because of the number of links is gonna be way larger than 3, do you know a way to make this asynchronous? The problem is that I MUST use a prepared request ('cause I'll use it in other functions as well) as shown here: https://docs.python-requests.org/en/latest/user/advanced/#prepared-requests, but I can't find the async equivalent. Here's the code:

import requests
import time

def get_website(method, url, session=requests.Session(), files=None,
            headers=None, cookies=None, data=None, timeout=42):
    req = requests.Request(
        method.upper(),
        url=url,
        data=data,
        files=files,
        headers=headers,
        cookies=cookies)
    prepped = req.prepare()
    return session.send(prepped, timeout=timeout)

with requests.Session() as s:
    links = ["http://link1", "http://link2", "http://link3"]
    for link in links:
        start = time.time()
        r = website("POST", url, session=s)
        print(r, f"{time.time() - start:.2f}")
    print("DONE")
0 Answers
Related