Python 当我转到concurrent.futures时,无法在函数中使用项对
我试图在下面的脚本中使用Python 当我转到concurrent.futures时,无法在函数中使用项对,python,python-3.x,concurrent.futures,Python,Python 3.x,Concurrent.futures,我试图在下面的脚本中使用concurrent.futures。其中有两个函数make_requests()和get_info()。当make_requests()函数产生链接和代理列表时,我打算在get_info()函数中传递它们 通常是这样的: if __name__ == '__main__': # "proxy_lists" is the holder of different proxies proxies = [proxy_lists] # make_requ
concurrent.futures
。其中有两个函数make_requests()
和get_info()
。当make_requests()
函数产生链接和代理列表时,我打算在get_info()
函数中传递它们
通常是这样的:
if __name__ == '__main__':
# "proxy_lists" is the holder of different proxies
proxies = [proxy_lists]
# make_requests() function yields "links" and "proxy_list"
for item,elem in make_requests(url,proxies):
# pass "links" and "proxy_list" to get_info() function
get_info(item,elem)
我没有发现使用concurrent.futures
做同样的事情的想法,因为它需要一个成对的循环,通过这个make\u requests()
函数生成两种类型的元素,然后在get\u info()
函数中传递这两种类型的元素,就像我上面做的那样:
import concurrent.futures
if __name__ == '__main__':
proxies = [proxy_lists]
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
future_to_url = {executor.submit(get_info,link): link for link in make_requests(url,proxies)}
concurrent.futures.as_completed(future_to_url)
当我使用concurrent.futures
时,如何传递get\u info()
函数中的一对项目?我想你只想做:executor.submit(get\u info,*link)
这是因为记录如下:
submit(fn、*args、**kwargs)
将可调用的fn调度为fn(*args**kwargs)
这使用标准来确保一切都经过fn
*链接
将链接
视为一个序列,将其解包为提交
的参数,然后当它最终调用fn