-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrd.py
51 lines (37 loc) · 1.3 KB
/
thrd.py
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
45
46
47
48
49
50
51
#threading
# perform multiple task without any problem
import threading
from queue import Queue
import time
print_lock = threading.Lock()
def exampleJob(worker):
time.sleep(.5) # pretend to do some work.
with print_lock:
print(threading.current_thread().name,worker)
# The threader thread pulls an worker from the queue and processes it
def threader():
while True:
# gets an worker from the queue
worker = q.get()
# Run the example job with the avail worker in queue (thread)
exampleJob(worker)
# completed with the job
q.task_done()
# Create the queue and threader
q = Queue()
# how many threads are we going to allow for
for x in range(10):
t = threading.Thread(target=threader)
# classifying as a daemon, so they will die when the main dies
t.daemon = True
# begins, must come after daemon definition
t.start()
start = time.time()
# 20 jobs assigned.
for worker in range(10):
q.put(worker)
# wait until the thread terminates.
q.join()
# with 10 workers and 20 tasks, with each task being .5 seconds, then the completed job
# is ~1 second using threading. Normally 20 tasks with .5 seconds each would take 10 seconds.
print('Entire job took:',time.time() - start)