Skip to content

Commit c439f57

Browse files
committed
Fixed most TR feedback for threading article
1 parent c38773d commit c439f57

File tree

12 files changed

+87
-109
lines changed

12 files changed

+87
-109
lines changed

intro_to_threading/bar.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

intro_to_threading/daemon_thread.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
import threading
44
import time
55

6+
67
def thread_function(name):
78
logging.info("Thread %s: starting", name)
89
time.sleep(2)
910
logging.info("Thread %s: finishing", name)
1011

1112

1213
if __name__ == "__main__":
13-
format='%(asctime)s: %(message)s'
14-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
14+
format = "%(asctime)s: %(message)s"
15+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
1516

1617
logging.info("Main : before creating thread")
17-
x = threading.Thread(target=thread_function, args=[1,], daemon=True)
18+
x = threading.Thread(target=thread_function, args=(1,), daemon=True)
1819
logging.info("Main : before running thread")
1920
x.start()
2021
logging.info("Main : wait for the thread to finish")
2122
x.join()
2223
logging.info("Main : all done")
23-

intro_to_threading/deadlock.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
import threading
3+
4+
l = threading.RLock()
5+
print("before first acquire")
6+
l.acquire()
7+
print("before second acquire")
8+
l.acquire()
9+
print("acquired lock twice")

intro_to_threading/executor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import threading
55
import time
66

7+
78
def thread_function(name):
89
logging.info("Thread %s: starting", name)
910
time.sleep(2)
1011
logging.info("Thread %s: finishing", name)
1112

1213

1314
if __name__ == "__main__":
14-
format='%(asctime)s: %(message)s'
15-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
15+
format = "%(asctime)s: %(message)s"
16+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
1617

17-
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
18+
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
1819
executor.map(thread_function, range(3))
19-

intro_to_threading/fixrace.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import threading
55
import time
66

7-
class FakeDatabase():
7+
8+
class FakeDatabase:
89
def __init__(self):
910
self.value = 0
1011
self._lock = threading.Lock()
@@ -22,13 +23,16 @@ def locked_update(self, name):
2223
logging.debug("Thread %s after release", name)
2324
logging.info("Thread %s: finishing update", name)
2425

26+
2527
if __name__ == "__main__":
26-
format='%(asctime)s: %(message)s'
27-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
28+
format = "%(asctime)s: %(message)s"
29+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
2830
# logging.basicConfig(format=format, level=logging.DEBUG, datefmt='%H:%M:%S')
2931

3032
database = FakeDatabase()
31-
logging.info("Testing locked update. Starting value is %d.", database.value)
33+
logging.info(
34+
"Testing locked update. Starting value is %d.", database.value
35+
)
3236
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
3337
for index in range(2):
3438
executor.submit(database.locked_update, index)

intro_to_threading/multiple_threads.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
import threading
44
import time
55

6+
67
def thread_function(name):
78
logging.info("Thread %s: starting", name)
89
time.sleep(2)
910
logging.info("Thread %s: finishing", name)
1011

1112

1213
if __name__ == "__main__":
13-
format='%(asctime)s: %(message)s'
14-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
14+
format = "%(asctime)s: %(message)s"
15+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
1516

1617
threads = list()
1718
for index in range(3):
1819
logging.info("Main : create and start thread %d.", index)
19-
x = threading.Thread(target=thread_function, args=[index, ])
20+
x = threading.Thread(target=thread_function, args=(index,))
2021
threads.append(x)
2122
x.start()
2223

intro_to_threading/prodcom_better_queue.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@
66
import threading
77
import time
88

9+
910
def producer(queue, event):
10-
'''Pretend we're getting a number from the network.'''
11+
"""Pretend we're getting a number from the network."""
1112
while not event.is_set():
12-
message = random.randint(1,101)
13+
message = random.randint(1, 101)
1314
logging.info("Producer got message: %s", message)
1415
queue.put(message)
1516

1617
logging.info("Producer received event. Exiting")
1718

19+
1820
def consumer(queue, event):
19-
''' Pretend we're saving a number in the database. '''
21+
""" Pretend we're saving a number in the database. """
2022
while not event.is_set() or not pipeline.empty():
2123
message = queue.get()
22-
logging.info("Consumer storing message: %s (size=%d)", message,
23-
queue.qsize())
24+
logging.info(
25+
"Consumer storing message: %s (size=%d)", message, queue.qsize()
26+
)
2427

2528
logging.info("Consumer received event. Exiting")
2629

30+
2731
if __name__ == "__main__":
28-
format='%(asctime)s: %(message)s'
29-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
32+
format = "%(asctime)s: %(message)s"
33+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
3034

3135
pipeline = queue.Queue(maxsize=10)
3236
event = threading.Event()
@@ -37,5 +41,3 @@ def consumer(queue, event):
3741
time.sleep(0.1)
3842
logging.info("Main: about to set event")
3943
event.set()
40-
41-

intro_to_threading/prodcom_event.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import time
77

88

9-
class Pipeline():
10-
'''Class to allow a single element pipeline between producer and consumer.
11-
'''
9+
class Pipeline:
10+
"""Class to allow a single element pipeline between producer and consumer.
11+
"""
12+
1213
def __init__(self):
1314
self.value = 0
1415
self._set_lock = threading.Lock()
@@ -34,10 +35,11 @@ def set_value(self, value, name):
3435
self._get_lock.release()
3536
logging.debug("%s:getlock released", name)
3637

38+
3739
def producer(pipeline, event):
38-
'''Pretend we're getting a number from the network.'''
40+
"""Pretend we're getting a number from the network."""
3941
while not event.is_set():
40-
new_datapoint = random.randint(1,101)
42+
new_datapoint = random.randint(1, 101)
4143
# Sleep to simulate waiting for data from network
4244
# time.sleep(float(new_datapoint)/100)
4345
logging.info("Producer got data %d", new_datapoint)
@@ -53,7 +55,7 @@ def producer(pipeline, event):
5355

5456

5557
def consumer(pipeline, event):
56-
''' Pretend we're saving a number in the database. '''
58+
""" Pretend we're saving a number in the database. """
5759
datapoint = 0
5860
while not event.is_set():
5961
datapoint = pipeline.get_value("Consumer")
@@ -63,9 +65,9 @@ def consumer(pipeline, event):
6365

6466

6567
if __name__ == "__main__":
66-
format='%(asctime)s: %(message)s'
68+
format = "%(asctime)s: %(message)s"
6769
# logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
68-
logging.basicConfig(format=format, level=logging.DEBUG, datefmt='%H:%M:%S')
70+
logging.basicConfig(format=format, level=logging.DEBUG, datefmt="%H:%M:%S")
6971

7072
pipeline = Pipeline()
7173
event = threading.Event()

intro_to_threading/prodcom_lock.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
SENTINEL = -1
99

10-
class Pipeline():
11-
'''Class to allow a single element pipeline between producer and consumer.
12-
'''
10+
11+
class Pipeline:
12+
"""Class to allow a single element pipeline between producer and consumer.
13+
"""
14+
1315
def __init__(self):
1416
self.message = 0
1517
self.producer_lock = threading.Lock()
@@ -36,28 +38,29 @@ def set_message(self, message, name):
3638
logging.debug("%s:getlock released", name)
3739

3840

39-
4041
def producer(pipeline):
41-
'''Pretend we're getting a message from the network.'''
42+
"""Pretend we're getting a message from the network."""
4243
for index in range(10):
43-
message = random.randint(1,101)
44+
message = random.randint(1, 101)
4445
logging.info("Producer got message: %s", message)
4546
pipeline.set_message(message, "Producer")
4647

4748
# send a sentinel message to tell consumer we're done
4849
pipeline.set_message(SENTINEL, "Producer")
4950

51+
5052
def consumer(pipeline):
51-
''' Pretend we're saving a number in the database. '''
53+
""" Pretend we're saving a number in the database. """
5254
message = 0
5355
while message != SENTINEL:
5456
message = pipeline.get_message("Consumer")
5557
if message != SENTINEL:
5658
logging.info("Consumer storing message: %s", message)
5759

60+
5861
if __name__ == "__main__":
59-
format='%(asctime)s: %(message)s'
60-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
62+
format = "%(asctime)s: %(message)s"
63+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
6164
# logging.basicConfig(format=format, level=logging.DEBUG, datefmt='%H:%M:%S')
6265

6366
pipeline = Pipeline()

intro_to_threading/prodcom_queue.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,49 @@
66
import threading
77
import time
88

9-
class Pipeline():
10-
def __init__(self):
11-
self.Q = queue.Queue(maxsize=10)
12-
13-
def empty(self):
14-
return self.Q.empty()
159

16-
def size(self):
17-
return self.Q.qsize()
10+
class Pipeline(queue.Queue):
11+
def __init__(self):
12+
super().__init__(maxsize=10)
1813

1914
def get_message(self, name):
2015
logging.debug("%s:about to get from queue", name)
21-
value = self.Q.get()
16+
value = self.get()
2217
logging.debug("%s:got %d from queue", name, value)
2318
return value
2419

2520
def set_message(self, value, name):
2621
logging.debug("%s:about to add %d to queue", name, value)
27-
self.Q.put(value)
22+
self.put(value)
2823
logging.debug("%s:added %d to queue", name, value)
2924

3025

3126
def producer(pipeline, event):
32-
'''Pretend we're getting a number from the network.'''
27+
"""Pretend we're getting a number from the network."""
3328
while not event.is_set():
34-
message = random.randint(1,101)
29+
message = random.randint(1, 101)
3530
logging.info("Producer got message: %s", message)
3631
pipeline.set_message(message, "Producer")
3732

3833
logging.info("Producer received EXIT event. Exiting")
3934

35+
4036
def consumer(pipeline, event):
41-
''' Pretend we're saving a number in the database. '''
37+
""" Pretend we're saving a number in the database. """
4238
while not event.is_set() or not pipeline.empty():
4339
message = pipeline.get_message("Consumer")
44-
logging.info("Consumer storing message: %s (queue size=%s)", message,
45-
pipeline.size())
40+
logging.info(
41+
"Consumer storing message: %s (queue size=%s)",
42+
message,
43+
pipeline.qsize(),
44+
)
4645

4746
logging.info("Consumer received EXIT event. Exiting")
4847

4948

5049
if __name__ == "__main__":
51-
format='%(asctime)s: %(message)s'
52-
logging.basicConfig(format=format, level=logging.INFO, datefmt='%H:%M:%S')
50+
format = "%(asctime)s: %(message)s"
51+
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")
5352
# logging.basicConfig(format=format, level=logging.DEBUG, datefmt='%H:%M:%S')
5453

5554
pipeline = Pipeline()

0 commit comments

Comments
 (0)