May 2018
Beginner to intermediate
452 pages
11h 26m
English
Before editing our application code, let's create a simpler example application to make sure we understand how to use Queue to communicate between threads.
Start with a long-running thread:
from threading import Thread
from time import sleep
class Backend(Thread):
def __init__(self, queue, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queue = queue
def run(self):
self.queue.put('ready')
for n in range(1, 5):
self.queue.put(f'stage {n}')
print(f'stage {n}')
sleep(2)
self.queue.put('done')
The Backend object is a subclass of Thread that takes a Queue object as an argument and saves it as an instance property. Its run method simulates a long-running four-phase process using print() and ...