如果没有关于您正在做什么的更多信息,很难肯定地说,但简单的线程方法可能有意义.
假设您有一个处理单个ID的简单函数:
import requests
url_t = "http://localhost:8000/records/%i"
def process_id(id):
"""process a single ID"""
# fetch the data
r = requests.get(url_t % id)
# parse the JSON reply
data = r.json()
# and update some data with PUT
requests.put(url_t % id, data=data)
return data
您可以将其扩展为处理一系列ID的简单函数:
def process_range(id_range, store=None):
"""process a number of ids, storing the results in a dict"""
if store is None:
store = {}
for id in id_range:
store[id] = process_id(id)
return store
最后,您可以相当轻松地将子范围映射到线程上,以允许一些请求并发:
from threading import Thread
def threaded_process_range(nthreads, id_range):</