Am I interpreting that right?
You observe such behavior because you’re modifying the same object all the time
Lets put aside queues / threads and run a simplified equivalent of your code with some prints to understand what’s happening
t = {}
l = []
for i in range(3):
t['a'] = i
l.append(t)
print(l)
t['a'] = 20
print(l)
print(map(id, l))
[{'a': 2}, {'a': 2}, {'a': 2}]
[{'a': 20}, {'a': 20}, {'a': 20}]
# they are all the same!
[4474861840, 4474861840, 4474861840]
So it has nothing to do we threads/queues - you’re just adding the same object 3 times.
Now, if I move the instantiation of the dictionary inside the for loop
In this case you create a new object every time like in the following code:
l = []
for i in range(3):
t = {}
t['a'] = i
l.append(t)
print(l)
t['a'] = 20
print(l)
print(map(id, l))
[{'a': 0}, {'a': 1}, {'a': 2}]
[{'a': 0}, {'a': 1}, {'a': 20}]
# they are all different!
[4533475600, 4533502592, 4533502872]
So no magic here
back to your question
This is what could be of interest for you: “Is python’s queue.Queue.put() thread safe?” meaning that the global variable q could be accessed by multiple concurrent threads safely. The answer is yes - it is thread safe
Answer from Oleg Kuralenko on Stack OverflowThe Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics
Videos
Am I interpreting that right?
You observe such behavior because you’re modifying the same object all the time
Lets put aside queues / threads and run a simplified equivalent of your code with some prints to understand what’s happening
t = {}
l = []
for i in range(3):
t['a'] = i
l.append(t)
print(l)
t['a'] = 20
print(l)
print(map(id, l))
[{'a': 2}, {'a': 2}, {'a': 2}]
[{'a': 20}, {'a': 20}, {'a': 20}]
# they are all the same!
[4474861840, 4474861840, 4474861840]
So it has nothing to do we threads/queues - you’re just adding the same object 3 times.
Now, if I move the instantiation of the dictionary inside the for loop
In this case you create a new object every time like in the following code:
l = []
for i in range(3):
t = {}
t['a'] = i
l.append(t)
print(l)
t['a'] = 20
print(l)
print(map(id, l))
[{'a': 0}, {'a': 1}, {'a': 2}]
[{'a': 0}, {'a': 1}, {'a': 20}]
# they are all different!
[4533475600, 4533502592, 4533502872]
So no magic here
back to your question
This is what could be of interest for you: “Is python’s queue.Queue.put() thread safe?” meaning that the global variable q could be accessed by multiple concurrent threads safely. The answer is yes - it is thread safe
The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics
In the first example, you are putting the same dict into the queue three times. This has nothing to do with the queue. You would find the same behaviour with list.append.