Basic asynchronous programming with threads
To utilize threading, we need to be able to start threads, allow them to run, and then join them. We can see the stages of practically managing our threads in the following diagram:
We start the threads, we then let them run, and once they have run, we join them. If we did not join them, the program would continue to run before the threads had finished. In Python, we create a thread by inheriting the Thread
object, as follows:
from threading import Thread from time import sleep from typing import Optional class ExampleThread(Thread): def __init__(self, seconds: int, name: str) -> None: super().__init__() self.seconds: int = seconds self.name: str = name ...