From a2c1d8bcc484eafcb381f404d290a1792db527fe Mon Sep 17 00:00:00 2001 From: Shraman Jain <60072287+Shraman-jain@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:42:07 +0530 Subject: [PATCH 1/5] Update index.md add threading.md --- contrib/advanced-python/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index b31544e..552025a 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -10,3 +10,4 @@ - [Protocols](protocols.md) - [Exception Handling in Python](exception-handling.md) - [Generators](generators.md) +- [Threading](threading.md) From ca4ce0620a9840af9e39f649e87166d4c0a895eb Mon Sep 17 00:00:00 2001 From: Shraman Jain <60072287+Shraman-jain@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:44:27 +0530 Subject: [PATCH 2/5] Create threading.md --- contrib/advanced-python/threading.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 contrib/advanced-python/threading.md diff --git a/contrib/advanced-python/threading.md b/contrib/advanced-python/threading.md new file mode 100644 index 0000000..85b5f4b --- /dev/null +++ b/contrib/advanced-python/threading.md @@ -0,0 +1 @@ +#Threading From fc5f366e6a64eac6948054af5c51bfee1a82a792 Mon Sep 17 00:00:00 2001 From: Shraman Jain <60072287+Shraman-jain@users.noreply.github.com> Date: Wed, 12 Jun 2024 00:27:10 +0530 Subject: [PATCH 3/5] Update threading.md --- contrib/advanced-python/threading.md | 138 ++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/contrib/advanced-python/threading.md b/contrib/advanced-python/threading.md index 85b5f4b..07bc4bf 100644 --- a/contrib/advanced-python/threading.md +++ b/contrib/advanced-python/threading.md @@ -1 +1,137 @@ -#Threading +# Threading in Python +Threading is a sequence of instructions in a program that can be executed independently of the remaining process and +Threads are like lightweight processes that share the same memory space but can execute independently. +The process is an executable instance of a computer program. +This guide provides an overview of the threading module and its key functionalities. + +## Key Characteristics of Threads: +* Shared Memory: All threads within a process share the same memory space, which allows for efficient communication between threads. +* Independent Execution: Each thread can run independently and concurrently. +* Context Switching: The operating system can switch between threads, enabling concurrent execution. + +## Threading Module +This module will allows you to create and manage threads easily. This module includes several functions and classes to work with threads. + +**1. Creating Thread:** +To create a thread in Python, you can use the Thread class from the threading module. + +Example: +```python +import threading + +# Create a thread +thread = threading.Thread() + +# Start the thread +thread.start() + +# Wait for the thread to complete +thread.join() + +print("Thread has finished execution.") +``` +**2. Performing Task with Thread:** +We can also perform a specific task by thread by giving a function as target and its argument as arg ,as a parameter to Thread object. + +Example: + +```python +import threading + +# Define a function that will be executed by the thread +def print_numbers(arg): + for i in range(arg): + print(f"Thread: {i}") +# Create a thread +thread = threading.Thread(target=print_numbers,args=(5,)) + +# Start the thread +thread.start() + +# Wait for the thread to complete +thread.join() + +print("Thread has finished execution.") +``` +**3. Delaying a Task with Thread's Timer Function:** +We can set a time for which we want a thread to start. Timer function takes 4 arguments (interval,function,args,kwargs). + +Example: +```python +import threading + +# Define a function that will be executed by the thread +def print_numbers(arg): + for i in range(arg): + print(f"Thread: {i}") +# Create a thread after 3 seconds +thread = threading.Timer(3,print_numbers,args=(5,)) + +# Start the thread +thread.start() + +# Wait for the thread to complete +thread.join() + +print("Thread has finished execution.") +``` +**4. Creating Multiple Threads** +We can create and manage multiple threads to achieve concurrent execution. + +Example: +```python +import threading + +def print_numbers(thread_name): + for i in range(5): + print(f"{thread_name}: {i}") + +# Create multiple threads +thread1 = threading.Thread(target=print_numbers, args=("Thread 1",)) +thread2 = threading.Thread(target=print_numbers, args=("Thread 2",)) + +# Start the threads +thread1.start() +thread2.start() + +# Wait for both threads to complete +thread1.join() +thread2.join() + +print("Both threads have finished execution.") +``` + +**5. Thread Synchronization** +When we create multiple threads and they access shared resources, there is a risk of race conditions and data corruption. To prevent this, you can use synchronization primitives such as locks. +A lock is a synchronization primitive that ensures that only one thread can access a shared resource at a time. + +Example: +```Python +import threading + +lock = threading.Lock() + +def print_numbers(thread_name): + for i in range(10): + with lock: + print(f"{thread_name}: {i}") + +# Create multiple threads +thread1 = threading.Thread(target=print_numbers, args=("Thread 1",)) +thread2 = threading.Thread(target=print_numbers, args=("Thread 2",)) + +# Start the threads +thread1.start() +thread2.start() + +# Wait for both threads to complete +thread1.join() +thread2.join() + +print("Both threads have finished execution.") +``` + +A ```lock``` object is created using threading.Lock() and The ```with lock``` statement ensures that the lock is acquired before printing and released after printing. This prevents other threads from accessing the print statement simultaneously. + +## Conclusion +Threading in Python is a powerful tool for achieving concurrency and improving the performance of I/O-bound tasks. By understanding and implementing threads using the threading module, you can enhance the efficiency of your programs. To prevent race situations and maintain data integrity, keep in mind that thread synchronization must be properly managed. From 5174c9dc1f8f93ab717ea5c0d0bb52176ebeac3f Mon Sep 17 00:00:00 2001 From: Shraman Jain <60072287+Shraman-jain@users.noreply.github.com> Date: Sat, 22 Jun 2024 23:30:23 +0530 Subject: [PATCH 4/5] Update threading.md --- contrib/advanced-python/threading.md | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/contrib/advanced-python/threading.md b/contrib/advanced-python/threading.md index 07bc4bf..fa31533 100644 --- a/contrib/advanced-python/threading.md +++ b/contrib/advanced-python/threading.md @@ -30,6 +30,10 @@ thread.join() print("Thread has finished execution.") ``` +Output : +``` +Thread has finished execution. +``` **2. Performing Task with Thread:** We can also perform a specific task by thread by giving a function as target and its argument as arg ,as a parameter to Thread object. @@ -53,6 +57,15 @@ thread.join() print("Thread has finished execution.") ``` +Output : +``` +Thread: 0 +Thread: 1 +Thread: 2 +Thread: 3 +Thread: 4 +Thread has finished execution. +``` **3. Delaying a Task with Thread's Timer Function:** We can set a time for which we want a thread to start. Timer function takes 4 arguments (interval,function,args,kwargs). @@ -75,6 +88,16 @@ thread.join() print("Thread has finished execution.") ``` +Output : +``` +# after three second output will be generated +Thread: 0 +Thread: 1 +Thread: 2 +Thread: 3 +Thread: 4 +Thread has finished execution. +``` **4. Creating Multiple Threads** We can create and manage multiple threads to achieve concurrent execution. @@ -100,6 +123,20 @@ thread2.join() print("Both threads have finished execution.") ``` +Output : +``` +Thread 1: 0 +Thread 1: 1 +Thread 2: 0 +Thread 1: 2 +Thread 1: 3 +Thread 2: 1 +Thread 2: 2 +Thread 2: 3 +Thread 2: 4 +Thread 1: 4 +Both threads have finished execution. +``` **5. Thread Synchronization** When we create multiple threads and they access shared resources, there is a risk of race conditions and data corruption. To prevent this, you can use synchronization primitives such as locks. @@ -130,6 +167,30 @@ thread2.join() print("Both threads have finished execution.") ``` +Output : +``` +Thread 1: 0 +Thread 1: 1 +Thread 1: 2 +Thread 1: 3 +Thread 1: 4 +Thread 1: 5 +Thread 1: 6 +Thread 1: 7 +Thread 1: 8 +Thread 1: 9 +Thread 2: 0 +Thread 2: 1 +Thread 2: 2 +Thread 2: 3 +Thread 2: 4 +Thread 2: 5 +Thread 2: 6 +Thread 2: 7 +Thread 2: 8 +Thread 2: 9 +Both threads have finished execution. +``` A ```lock``` object is created using threading.Lock() and The ```with lock``` statement ensures that the lock is acquired before printing and released after printing. This prevents other threads from accessing the print statement simultaneously.