Sunday, March 19, 2023

What is synchronisation in python

 In Python, synchronization refers to the coordination of multiple threads or processes to ensure that they access shared resources in a safe and consistent way.




In a multi-threaded or multi-process program, it is possible for different threads or processes to access shared data simultaneously, which can lead to race conditions, data corruption, and other errors. Synchronization techniques are used to prevent these problems by controlling access to shared resources.


Python provides several synchronization mechanisms, including locks, semaphores, and conditions. These mechanisms allow threads or processes to coordinate their access to shared resources, ensuring that only one thread or process can access the resource at a time.


For example, a lock can be used to ensure that only one thread at a time can modify a shared variable, while other threads wait for the lock to be released. Similarly, a semaphore can be used to limit the number of threads that can access a shared resource at the same time.


Synchronization is an important concept in concurrent programming, and it is essential for building reliable, scalable, and efficient multi-threaded or multi-process applications in Python.

Synchronous process using multiprocessing 

Multiprocessing allows you to run multiple processes simultaneously on multiple CPUs or CPU cores. A synchronous process using multiprocessing is one in which the processes are executed in a synchronized manner. This means that the processes must wait for each other to finish before moving on to the next step.


Here is an example of a synchronous process using multiprocessing in Python:

import multiprocessing


def worker(num):

    """worker function"""

    print('Worker:', num)

    return


if __name__ == '__main__':

    jobs = []

    for i in range(5):

        p = multiprocessing.Process(target=worker, args=(i,))

        jobs.append(p)

        p.start()

    

    # wait for all processes to finish

    for job in jobs:

        job.join()


In this example, we define a worker function that takes a number as an argument and prints a message indicating that it is executing. We then create five processes using the multiprocessing module, with each process executing the worker function with a different number as an argument. We store each process in a list of jobs and start each process using the start() method.


After starting all the processes, we use a for loop to iterate through the list of jobs and call the join() method on each process. This causes the main program to wait until all the processes have completed before moving on to the next step. This ensures that the processes are executed in a synchronized manner.


Note that the if __name__ == '__main__': block is necessary in this example because it ensures that the multiprocessing module is only imported and executed once, preventing issues that can occur when multiprocessing is used in conjunction with certain IDEs or interactive shells.






Featured Post

Python for Data Science: An Introduction to Pandas

Python has become the go-to language for data science due to its simplicity, flexibility, and powerful libraries. One such library is Pandas...

Popular Posts