1

how to manipulate dict with thread-safe, i want to store dict to 2 different queue with different dict value (1 before manipulation, 1 after manipulation) but turns out both of them store dict after manipulation.

here's my code

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)

the result of code above is like this

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

but i want the B row only contain 'event' without 'dt'. so here's what i want

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

1 Answers1

1

When you do q_val = val you copy the reference of the dictionary you don't create a copy of the variable. So q_val and val are actually the same dictionary.

Assignment statements in Python do not copy objects, they create bindings between a target and an object

If you want to copy a variable in python use copy.copy().

q_val = copy.copy(val)
Yassine Faris
  • 951
  • 6
  • 26