Skip to content

Commit 171c567

Browse files
me
1 parent c3c8ded commit 171c567

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

contrib/advanced-python/MultiThreadingg.md

+28-28
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
for i in range(1, 6):
2323
print(i)
2424

25-
# Create a thread
26-
thread = threading.Thread(target=print_numbers)
25+
# Create a thread
26+
thread = threading.Thread(target=print_numbers)
2727

28-
# Start the thread
29-
thread.start()
28+
# Start the thread
29+
thread.start()
3030

31-
# Wait for the thread to complete
32-
thread.join()
31+
# Wait for the thread to complete
32+
thread.join()
3333

3434
- Synchronizing Threads
3535
When multiple threads access shared resources, synchronization is necessary to avoid data corruption. The threading module provides
@@ -46,22 +46,22 @@ thread.join()
4646
for i in range(1, 6):
4747
print(i)
4848

49-
# Create multiple threads
50-
threads = [threading.Thread(target=print_numbers) for _ in range(3)]
49+
# Create multiple threads
50+
threads = [threading.Thread(target=print_numbers) for _ in range(3)]
5151

52-
# Start the threads
53-
for thread in threads:
54-
thread.start()
52+
# Start the threads
53+
for thread in threads:
54+
thread.start()
5555

56-
# Wait for all threads to complete
57-
for thread in threads:
58-
thread.join()
56+
# Wait for all threads to complete
57+
for thread in threads:
58+
thread.join()
5959

60-
- Thread Communication
61-
Threads can communicate using shared variables, but this requires careful synchronization. Another approach is to use thread-safe data
62-
structures like Queue from the queue module.
60+
- Thread Communication
61+
Threads can communicate using shared variables, but this requires careful synchronization. Another approach is to use thread-safe data
62+
structures like Queue from the queue module.
6363

64-
** Example using Queue
64+
** Example using Queue
6565

6666
import threading
6767
import queue
@@ -74,20 +74,20 @@ for thread in threads:
7474

7575
q = queue.Queue()
7676

77-
# Add items to the queue
78-
for item in range(1, 11):
77+
# Add items to the queue
78+
for item in range(1, 11):
7979
q.put(item)
8080

81-
# Create and start worker threads
82-
threads = [threading.Thread(target=worker, args=(q,)) for _ in range(3)]
81+
# Create and start worker threads
82+
threads = [threading.Thread(target=worker, args=(q,)) for _ in range(3)]
8383

84-
for thread in threads:
85-
thread.start()
84+
for thread in threads:
85+
thread.start()
8686

87-
# Wait for all tasks to be processed
88-
q.join()
89-
Example: Multithreading in Python
90-
Let's create a more comprehensive example to demonstrate multithreading in a real-world scenario.
87+
# Wait for all tasks to be processed
88+
q.join()
89+
Example: Multithreading in Python
90+
Let's create a more comprehensive example to demonstrate multithreading in a real-world scenario.
9191

9292
- Example: Downloading Multiple URLs
9393

0 commit comments

Comments
 (0)