如何对请求使用多线程?

时间:2019-08-02 17:56:42

标签: python python-3.x multithreading python-2.7 python-requests

你好,我有使用Python的代码,该代码使用了requests模块:

import requests

url1 = "myurl1" # I do not remember exactly the exact url
reponse1 = requests.get(url1)
temperature1 = reponse1.json()["temperature"]

url2 = "myurl2" # I do not remember exactly the exact url
reponse2 = requests.get(url2)
temperature2 = reponse2.json()["temp"]

url3 = "myurl3" # I do not remember exactly the exact url
reponse3 = requests.get(url3)
temperature3 = reponse3.json()[0]

print(temperature1)
print(temperature2)
print(temperature3)

实际上我必须告诉您这有点慢...您有解决方案来提高我的代码速度吗?我以为使用多线程,但我不知道该如何使用...

非常感谢您!

1 个答案:

答案 0 :(得分:0)

尝试Python executors

import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
from multiprocessing import cpu_count

urls = ['/url1', '/url2', '/url3']
with ThreadPoolExecutor(max_workers=2*cpu_count()) as executor:
    future_to_url = {executor.submit(requests.get, url): url for url in urls}
    for future in as_completed(future_to_url):
        response = future.result()  # TODO: handle exceptions here
        url = future_to_url[future]
        # TODO: do something with that data