Python 如何对请求使用多线程处理?
您好,我有一段使用Python的代码,它使用请求模块:Python 如何对请求使用多线程处理?,python,python-3.x,multithreading,python-2.7,python-requests,Python,Python 3.x,Multithreading,Python 2.7,Python Requests,您好,我有一段使用Python的代码,它使用请求模块: 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 = r
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)
事实上我得告诉你这有点慢。。。你有没有办法提高我的代码速度?我想使用多线程,但我不知道如何使用它
多谢各位 试试看:
我想如果你做一些搜索,这里会有问答,因此有使用多处理、线程、异步IO或cuncurrent.futures.Related运行web请求的示例:。。。还有更多。concurrent.futures文档甚至有指向其他解决方案的要点。concurrent.futures的可能复制使它变得简单。!
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