Life Cycle of a Thread in Python
Life Cycle of a Thread in Python
1. New (Born)
t = Thread()→
t.start() →2. Runnable (Ready)
Waiting for CPU
Waiting for CPU
→ Scheduler →
3. Running
Executing
Executing
run()→ Done →
5. Terminated
(Dead State)
(Dead State)
4. Blocked / Waiting State (Triggered by
sleep(), join(), or waiting for Lock.acquire())
Explanation
- Calling start schedules the thread's work and its target may run or wait.
- The thread finishes when its target returns or exits through an unhandled exception.
How Thread Synchronization Works with Lock
How Thread Synchronization Works with Lock
1. Acquire Lock
(Door Locked 🔒)
lock.acquire()(Door Locked 🔒)
→
2. Critical Section
Only 1 Thread Updates Shared Data
Only 1 Thread Updates Shared Data
→
3. Release Lock
(Door Unlocked 🔓)
lock.release()(Door Unlocked 🔓)
Explanation
- A thread acquires the lock before changing shared state.
- Other threads using that lock wait until the complete protected update is finished.
5 Steps of Python Database Communication (PDBC)
5 Steps of Python Database Communication (PDBC)
Step 1: Import Driver
import sqlite3or
mysql.connectorStep 2: Connect
con = connect(...)Establishes DB link
Step 3: Create Cursor
cur = con.cursor()SQL execution handle
Step 4: Execute SQL
cur.execute(sql)&
con.commit()Step 5: Close
cur.close()con.close()Explanation
- Open a connection, create a cursor and execute a parameterised statement.
- Read or commit the result as appropriate and release the resources afterwards.
The Golden Trio of Python Data Science Libraries
The Golden Trio of Python Data Science Libraries
1. NumPy (Numerical Python)
Fast multi-dimensional arrays (
ndarray), matrices, and linear algebra calculations.2. Pandas (Panel Data)
Tabular data manipulation using
Series (1D) and DataFrame (2D Excel-like tables).3. Matplotlib
Data visualization library for drawing 2D graphs (Line, Bar, Scatter, Histogram, Pie charts).
Explanation
- NumPy works with numerical arrays and pandas organises labelled data.
- Matplotlib turns the selected values into a plot for interpretation.