'Synchronisation in python
I have been trying to find a good way to implement synchronisation in Python. Is the following code alright? If no, then what is wrong with it and how can I improve it?
import threading
class BookingService(threading.Thread):
# so lock remains static to the class and would be same for all the threads
lock = None
def __init__(self,name):
threading.Thread.__init__(self)
self.name = name
if BookingService.lock == None:
BookingService.lock = threading.Lock()
def book(self):
with BookingService.lock:
print("BOOKED! for",self.name)
b1 = BookingService("b1")
b2 = BookingService("b2")
b1.book()
b2.book()
Read about threading module and various ways by which we can do it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
