-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrentThreading.py
More file actions
44 lines (37 loc) · 1.06 KB
/
Copy pathconcurrentThreading.py
File metadata and controls
44 lines (37 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
35
36
37
38
39
40
41
42
43
44
import threading
import time
def crawl(link, delay=1):
print("Crawl is executing 1")
time.sleep(delay)
print("Crawl is executing 2")
time.sleep(delay)
print("Crawl is executing 3")
time.sleep(delay)
print("Crawl is executing 4")
def drawl(link, delay=1):
print("Drawl is executing 1")
time.sleep(delay)
print("Drawl is executing 2")
time.sleep(delay)
print("Drawl is executing 3")
time.sleep(delay)
print("Drawl is executing 4")
links = [
"https://python.org",
"https://docs.python.org",
"https://peps.python.org",
]
# Start threads for each link
threads = []
for link in links:
# Using `args` to pass positional arguments and `kwargs` for keyword arguments
t = threading.Thread(target=crawl, args=(link,), kwargs={"delay": 2})
n = threading.Thread(target=drawl, args=(link, ), kwargs={"delay": 1})
threads.append(t)
threads.append(n)
# Start each thread
for t in threads:
t.start()
# Wait for all threads to finish
for t in threads:
t.join()