I'm playing with threading.Lock
and wrote the following test to further my understanding.
import threading
from time import sleep
LOCK = threading.Lock()
class Printer(threading.Thread):
def __init__(self, *args, **kwargs):
super(Printer, self).__init__(*args, **kwargs)
self.i = 0
def run(self):
while True:
print(self.i)
self.i += 1
sleep(1)
p = Printer()
p.start()
raw_input() # block main thread until return pressed
LOCK.acquire()
# stop printer for 6 seconds
for _ in range(3):
print('holding...')
sleep(2)
# let printer continue
LOCK.release()
I expected the following output (comment not part of output):
0
1
2
# user pressed enter
holding...
holding...
holding...
3
4
5
...
However, the printer does not stop - the actual output I am getting is:
0
1
2
# user pressed enter
holding...
3
4
holding...
5
6
holding...
7
8
...
Obviously I am misunderstanding something here? How can I temporarily suspend the printer?