Python provides a synchronization mechanism through Lock objects in its threading package. To learn more about locks or thread synchronization in general, have a look at here. In this post, I will demonstrate how we can make synchronize access to a shared piece of data such as a list in Python. Let’s start with the code first.
myList = 
myListLock = threading.Lock()
toret = ''
toret = myList[pos]
This demonstrates a very simplified implementation of how we can establish synchronized access to a shared resource which, in this case, is the list. Note that each of the function wraps any read or write access within the pair of acquire() and release() methods of the lock. In a multi-threaded environment, these functions must be made global so that all the threads can access the contents of the list in a synchronized manner with these functions only. The list is still global, so accessing its elements bypassing these functions is definitely possible. But we want everything to be synchronized right? So we should avoid doing that. Another way to keep global access available is to have all these wrapped in class with static members functions for synchronized access to the list.