0
我需要并行运行下列循环n次:即 一次迭代不应该等待其前面的迭代完成。 我希望所有的迭代能够同时运行'n'的所有值,这样我就可以减少运行时间并更快地获得结果。我应该在这个python代码中使用哪种方法..多线程或多处理?
for i in range(0,n):
api=str('some API string like https://maps.googleapis.com/maps/api/place/details/output?parameters '+offset+'')
tag=json_tag(api) #API call
temp=tag["data"] #parsing json response
for i in temp:
place_list.append(i["name"]) #storing name field in list
offset=int(offset)
offset+=50
其中
def jason_tag(string):
http = urllib3.PoolManager()
r = http.request('GET',string)
tag=json.loads(r.data.decode('utf8'))
return tag
我调用API 'N',这有一个 '偏移' 参数,从而改变每次当API调用时的时间..“json_tag(API)返回JSON API调用的响应。从json响应中获取一些数据并将其存储在名为'place_list'的列表中。
我想输出是same.i.e.列表中的内容(“place_list”)应该与我通常运行循环的顺序相同。我应该使用哪种方法来运行循环并行多处理或多线程? 我怎样才能实现它
不清楚你在做什么... –
@AmitGold我打电话给一个'n'次的API,它有一个'offset'参数,每次调用API时都会改变''json_tag(api)'返回json对API调用的响应。从json响应中获取一些数据并将其存储在名为'place_list'的列表中。 –