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. import threading myList = [] myListLock = threading.Lock() def addToList(item): myListLock.acquire() myList.append(item) myListLock.release() def removeFromList(item): myListLock.acquire() myList.remove(item) myListLock.release() def getItemAt(pos): toret = ” myListLock.acquire() toret = myList[pos] myListLock.release() return toret This demonstrates a very simplified implementation of how we can establish synchronized access …
[ Continue Reading... ]

Recent Comments