Skip to content

Commit 09418ba

Browse files
Update README.md
1 parent 4a0f442 commit 09418ba

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

09_Advanced_Topics/README.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,51 @@ Run parallel backups or deploy to multiple servers at once.
214214
### 🔹 **Multiprocessing**
215215

216216
**Definition:**
217-
Used to run multiple processes in **parallel**, taking advantage of multiple CPU cores — ideal for CPU-intensive tasks.
217+
Multiprocessing means running multiple processes simultaneously, where each process has its own Python interpreter and memory space.
218+
219+
> 👉 Unlike multithreading, multiprocessing bypasses the Global Interpreter Lock (GIL), allowing true parallel execution on multiple CPU cores.
218220
219221
### Example:
220222

221223
```python
222-
from multiprocessing import Pool
224+
import multiprocessing
225+
import time
223226

224-
def heavy_task(x):
225-
return x * x
227+
def square_numbers():
228+
for i in range(1, 6):
229+
print(f"Square of {i} is {i*i}")
230+
time.sleep(1)
231+
232+
def cube_numbers():
233+
for i in range(1, 6):
234+
print(f"Cube of {i} is {i*i*i}")
235+
time.sleep(1)
226236

227-
with Pool(4) as p:
228-
results = p.map(heavy_task, [1, 2, 3, 4])
229-
print(results)
237+
if __name__ == "__main__":
238+
# Creating processes
239+
p1 = multiprocessing.Process(target=square_numbers)
240+
p2 = multiprocessing.Process(target=cube_numbers)
241+
242+
# Starting processes
243+
p1.start()
244+
p2.start()
245+
246+
# Waiting for both to complete
247+
p1.join()
248+
p2.join()
249+
250+
print("✅ Both processes completed!")
251+
252+
```
253+
#### Output (order may vary):
254+
```
255+
Square of 1 is 1
256+
Cube of 1 is 1
257+
Square of 2 is 4
258+
Cube of 2 is 8
259+
✅ Both processes completed!
230260
```
261+
> Each process runs independently on different CPU cores — truly in parallel.
231262
232263
💡 **DevOps Use Case:**
233264
Process large datasets, compress logs, or analyze performance metrics in parallel.

0 commit comments

Comments
 (0)