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):
"""
process the id range in a specified number of threads
"""
store = {}
threads = []
# create the threads
for i in range(nthreads):
ids = id_range[i::nthreads]
t = Thread(target=process_range, args=(ids,store))
threads.append(t)
# start the threads
[t.start() for t in threads]
# wait for the threads to finish
[t.join() for t in threads]
return store
Last modification:July 16th, 2021 at 05:45 pm
© 允许规范转载