-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathU7_3_URL_Threading_Multi.py
More file actions
executable file
·35 lines (27 loc) · 1.06 KB
/
Copy pathU7_3_URL_Threading_Multi.py
File metadata and controls
executable file
·35 lines (27 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
####################################################
#
# Uebung:
# Erstellen Sie ein Programm, welches drei Threads startet
# Der erste Thread läuft 8 Sekunden, der zweite 4 Sekunden und der dritte 6 Sekunden
# Nehmen Sie als Vorlage , die vorhergehenden Folie.
#
####################################################
#### Lösung: ####
from time import perf_counter
from concurrent import futures
import urllib.parse, urllib.request
response=[]
wiki = 'https://de.wikipedia.org/wiki/'
laender = ['Schweiz', 'Deutschland', 'Frankreich', 'Italien', 'Österreich']
urls = [wiki + urllib.parse.quote(l) for l in laender]
def get_url(url):
try:
l = len(urllib.request.urlopen(url).read())
response.append(f"{url:45}{l:>10d}")
except: response.append(f"{url:45}Fehler")
start = perf_counter()
with futures.ThreadPoolExecutor() as e:
res = e.map(get_url, urls) ### Starten der Threads
for r in response:print (r) ### Resultat auslesen
end = perf_counter()
print(f"Performance: {end - start} Sec")