🐍 Advanced Python Programming β€” Thread Life CycleThread State β†’ State-driven Implementation β†’ Practical Thread Application
VLAB THREAD CELL
PYTHON CONCURRENCY

Observe the State. Control the Thread. Apply the Pattern.

Three progressive missions using the KASA Python Runtime.

πŸ–₯ Run the Thread

Run the supplied code, then replay the conceptual lifecycle animation.

Ready.

Thread Life Cycle Β· Conceptual View

Python's threading API does not expose a Java-style lifecycle enum. This visualization maps observable behavior to useful conceptual states.

NEW
β†’
RUNNING
β†’
WAITING
β†’
RUNNING
β†’
TERMINATED
Run the code to animate the lifecycle.
Key idea: create Thread β†’ start() β†’ work executes β†’ thread may wait/block β†’ resumes β†’ run() returns β†’ terminated. join() makes the caller wait for completion.

Mission Β· Report Worker

A report worker starts, reaches a gate because data is not ready, waits for a signal, resumes, then finishes.

β‘  NEW β€” create ReportWorker
β‘‘ RUNNING β€” call start()
β‘’ WAITING β€” worker calls gate.wait()
β‘£ RESUMED β€” Main calls gate.set()
β‘€ TERMINATED β€” worker completes; Main joins it
Target order: REPORT: RUNNING β†’ REPORT: WAITING β†’ MAIN: SIGNAL β†’ REPORT: RESUMED β†’ REPORT: TERMINATED β†’ MAIN: COMPLETE

πŸ–₯ Implement by State

Complete the TODOs so behavior follows the scenario.

Ready.

Real Case Β· Build a Dashboard Concurrently

A dashboard needs PROFILE, ORDERS and PAYMENT data. These are independent I/O-like tasks, so launch three threads and wait until all results are ready.

PROFILE
350 ms
ORDERS
200 ms
PAYMENT
280 ms
Thread per source β€” independent tasks overlap
Lock shared result β€” protect mutation of results
Join all β€” dashboard becomes READY only after all workers finish
Preview the concurrent flow, then implement it.

πŸ–₯ Practical Thread Challenge

Complete the thread creation, start and join phases.

Expected final state: DASHBOARD READY + 3 OK results.

πŸ”’ Instructor Solution

PBKDF2-SHA256 (150,000 iterations) + AES-GCM-256.