Concurrency and Modules
Starting Threads
The threading module runs a target function on another thread. Replay records
which thread produced each trace event, then colors that activity in the same
source pane.
Worker Function
thread_start_join.py
import threading
def worker(name, limit):
for step in range(1, limit + 1):
message = name + ":" + str(step)
print(message)
limit =
alpha = threading.Thread(target=worker, args=("alpha", limit))
beta = threading.Thread(target=worker, args=("beta", limit))
alpha.start()
beta.start()
alpha.join()
beta.join()
print("done")
import threading
def worker(name, limit):
for step in range(1, limit + 1):
message = name + ":" + str(step)
print(message)
limit =
alpha = threading.Thread(target=worker, args=("alpha", limit))
beta = threading.Thread(target=worker, args=("beta", limit))
alpha.start()
beta.start()
alpha.join()
beta.join()
print("done")
start and join
`start()` begins work on a new thread. `join()` waits until that thread has finished before the main thread continues.