如何做线程安全字典操作

时间:2018-04-11 15:47:46

标签: python multithreading python-3.x thread-safety python-multithreading

如何使用线程安全来操作dict,我想将dict存储到具有不同dict值的2个不同队列(操作前为1,操作后为1)但是在操作之后它们都存储了dict。

这是我的代码

from threading import Timer, Lock
from queue import Queue

q = Queue(maxsize=10000)
k = Queue(maxsize=10000)
k_lock = Lock()
q_lock = Lock()
def write_func(x):
    rows = []
    while not x.empty():
        rows.append(x.get())
    if len(rows) == 0:
        return
    print('A Row is {} and total A row is {}'.format(rows, len(rows)))

def write_k(x):
    rows = []
    while not x.empty():
        rows.append(x.get())
    if len(rows) == 0:
        return
    print('B Row is {} and total B row is {}'.format(rows, len(rows)))


def write_caller(sec=10):
    write_func(q)
    Timer(sec, write_caller, [sec]).start()


def k_caller(sec=1):
    write_k(k)
    Timer(sec, k_caller, [sec]).start()


write_caller()
k_caller()
while True:
    val = {"event": "lazada"}
    k.put(val)
    q_val = val
    with Lock():
        q_val["dt"] = "2018-04-11"
        q.put(q_val)
    if q.qsize() >= 10:
        write_func(q)

上面代码的结果就像这样

A Row is [{'event': 'lazada', 'dt': '2018-04-11'}, ...]
B Row is [{'event': 'lazada', 'dt': '2018-04-11'}, ...]

但我希望B行只包含没有'dt'的'event'。所以这就是我想要的东西

A Row is [{'event': 'lazada', 'dt': '2018-04-11'}, ...]
B Row is [{'event': 'lazada'}, ...]

1 个答案:

答案 0 :(得分:1)

当您q_val = val复制字典的引用时,您不会创建该变量的副本。所以q_val和val实际上是相同的字典。

  

Python中的赋值语句不复制对象,它们在目标和对象之间创建绑定

如果要在python中复制变量,请使用copy.copy()

q_val = copy.copy(val)