python调用ipython_使用Python(IPython)并行进行多个API调用

如果没有更多关于您正在做什么的信息,很难说是肯定的,但是一个简单的线程方法可能是有意义的。

假设您有一个处理单个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):

"""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

如果您的单个任务占用的时间范围更广,您可能希望使用ThreadPool,它将一次分配一个作业(如果单个任务非常小,通常会较慢,但在异构情况下可以保证更好的平衡)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值