Make multithreaded with ThreadPool
David Blume

David Blume commited on 2020-12-27 10:22:20
Showing 1 changed files, with 3 additions and 1 deletions.

... ...
@@ -2,6 +2,7 @@
2 2
 # From https://pybay.com/site_media/slides/raymond2017-keynote/process.html
3 3
 import urllib.request
4 4
 import urllib.error
5
+from multiprocessing.pool import ThreadPool
5 6
 
6 7
 sites = [
7 8
 #    'https://www.yahoo.com/',
... ...
@@ -32,7 +33,8 @@ def sitesize(url: str):
32 33
 
33 34
 
34 35
 def run():
35
-    for result in map(sitesize, sites):
36
+    pool = ThreadPool(2)
37
+    for result in pool.imap_unordered(sitesize, sites):
36 38
         print(result)
37 39
 
38 40
 
39 41