Last active
May 20, 2021 05:56
-
-
Save laixintao/7c6aa12c9e5e4ec96851e254f9ea2d7e to your computer and use it in GitHub Desktop.
list.append is an atomic action, so no lock needed when multiple threads append on same list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urls_to_download = ['baidu.com', 'taobao.com', 'zhihu.com', 'douban.com'] | |
done_count = 0 | |
done_count_lock = Lock() | |
def download(url): | |
requests.get(url) | |
with done_count_lock: | |
done_count += 1 | |
print(f"currnet process: {done_count} / {len(urls_to_download)}") | |
def main(): | |
with ThreadPoolExecutor(max_workers=2) as executor: | |
for url in urls_to_download: | |
future = executor.submit(download, url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urls_to_download = ['baidu.com', 'taobao.com', 'zhihu.com', 'douban.com'] | |
done_count = 0 | |
done_urls = [] | |
def download(url): | |
requests.get(url) | |
done_urls.append(url) # no lock needed | |
print(f"currnet process: {len(done_count)} / {len(urls_to_download)}") | |
def main(): | |
with ThreadPoolExecutor(max_workers=2) as executor: | |
for url in urls_to_download: | |
future = executor.submit(download, url) |
uranusjr
commented
May 20, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment