当我选择 concurrent.futures 时,无法在函数中使用一对项目
Unable to use pair of items within a function when I go for concurrent.futures
我正在尝试在下面的脚本中使用 concurrent.futures
。其中有两个函数 make_requests()
和 get_info()
。当 make_requests()
函数产生 links
和 proxy_list
时,我打算在 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_requests()
函数生成两种类型的元素,然后传递这两种类型的元素在 get_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_info()
函数中传递一对项目?
我想你只想做:executor.submit(get_info, *link)
这是因为 Executor.submit()
记录为:
submit(fn, *args, **kwargs)
Schedules the callable, fn, to be executed as fn(*args **kwargs)
这使用标准 arbitrary argument handling 来确保一切都通过 fn
*link
将 link
视为一个序列,将其解包为 submit
的参数,而当它最终调用 fn
我正在尝试在下面的脚本中使用 concurrent.futures
。其中有两个函数 make_requests()
和 get_info()
。当 make_requests()
函数产生 links
和 proxy_list
时,我打算在 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_requests()
函数生成两种类型的元素,然后传递这两种类型的元素在 get_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_info()
函数中传递一对项目?
我想你只想做:executor.submit(get_info, *link)
这是因为 Executor.submit()
记录为:
submit(fn, *args, **kwargs)
Schedules the callable, fn, to be executed as fn(*args **kwargs)
这使用标准 arbitrary argument handling 来确保一切都通过 fn
*link
将 link
视为一个序列,将其解包为 submit
的参数,而当它最终调用 fn