我有一个有字典的班级
class OrderBook:
orders = {'Restaurant1': None,
'Restaurant2': None,
'Restaurant3': None,
'Restaurant4': None}
@staticmethod
def addOrder(restaurant_name, orders):
OrderBook.orders[restaurant_name] = orders
我正在运行4个线程(每个餐厅一个) ,它们调用方法 OrderBook.addOrder
。下面是每个线程运行的函数:
def addOrders(restaurant_name):
#creates orders
...
OrderBook.addOrder(restaurant_name, orders)
这样安全吗? 还是在调用 addOrder
之前必须使用锁?