请答主尽量不要用“aiohttp、asyncio、aiofiles”,实在弄不明白。
具体的代码如下(串行),同样功能如何实现并行?
import requests
def getdata(num):
params={
'id':'123456789',
'num':num
}
url='http://httpbin.org/post'
r=requests.post(url,params=params).json()
return r['url']
def main():
nums=range(0,100)
urls=[]
for num in nums:
url=getdata(num)
urls.append(url)
print(urls)
main()
相关实现方法(仍未解决本人的问题)
我已知的能实现并行请求的简单方法如下,但是该方法的局限性是:必须先有URL列表,不能带参数,而实际开发当中需要带参数请求,虽然上面的问题,可以事先根据参数生成URL列表,但是我想知道事先无法生产URL列表的情况怎么实现并行,比如要提交form data时该怎么实现并行:
import requests
from multiprocessing.dummy import Pool as ThreadPool
urls = ['http://www.baidu.com']*20
# Make the Pool of workers
pool = ThreadPool(10)
# Open the urls in their own threads
# and return the results
results = pool.map(requests.get, urls)
#close the pool and wait for the work to finish
pool.close()
pool.join()
print(results)
想要快速获得别人的回答,尽量提付费问答。
你的问题是对进程池的使用方法不清楚,代下如下: