by FreakLegion · 3y ago · 0 comments
tasks = {}
for url in URLs:
future = executor.submit(fetch_url, url)
tasks[future] = url
bothers you, this is perfectly (some would say more so even than the original) Pythonic: tasks = {executor.submit(fetch_url, url): url for url in URLs} def main():
with ThreadPoolExecutor(max_workers=len(URLs)) as executor:
for url, title in zip(URLs, executor.map(fetch_url, URLs)):
print(f"URL: {url}\nTitle: {title}")
The default value of `max_workers` since
Python 3.8 has been min(32, os.cpu_count() + 4)
You should probably avoid max_workers=len(items_to_process)
It will not save memory or CPU time when you have few items (workers are created as necessary)
and may waste memory when you have many.