I'm writing a socket server that passes the server state to the client when requested. The state is passed as a JSON but in the server the data is stored in a dictionary. The socketserver handler pulls the server state from a queue (the server runs in a thread) when the client requests the server state.
My problem is that I always get only the final queue entry when I get from the queue. I can see the queue size decrementing as I pull entries from the queue. Here's a very simple script that illustrates the issue:
import queue
appState = {"console" : {"message" : "NA"}}
appStateQueue = queue.Queue()
print("Putting dict data on the queue:")
for i in range(10):
appState["console"]["message"] = str(i)
print(appState)
appStateQueue.put_nowait(appState)
print("Queue size:", appStateQueue.qsize())
print("Getting dict data from the queue:")
for i in range(10):
print(appStateQueue.get(), appStateQueue.qsize())
print("Putting integer data on the queue:")
for i in range(10):
appState = i
print(appState)
appStateQueue.put_nowait(appState)
print("Queue size:", appStateQueue.qsize())
print("Getting integer data from the queue:")
for i in range(10):
print(appStateQueue.get(), appStateQueue.qsize())
The output is as follows:
Putting dict data on the queue:
{'console': {'message': '0'}}
{'console': {'message': '1'}}
{'console': {'message': '2'}}
{'console': {'message': '3'}}
{'console': {'message': '4'}}
{'console': {'message': '5'}}
{'console': {'message': '6'}}
{'console': {'message': '7'}}
{'console': {'message': '8'}}
{'console': {'message': '9'}}
Queue size: 10
Getting dict data from the queue:
{'console': {'message': '9'}} 9
{'console': {'message': '9'}} 8
{'console': {'message': '9'}} 7
{'console': {'message': '9'}} 6
{'console': {'message': '9'}} 5
{'console': {'message': '9'}} 4
{'console': {'message': '9'}} 3
{'console': {'message': '9'}} 2
{'console': {'message': '9'}} 1
{'console': {'message': '9'}} 0
Putting integer data on the queue:
0
1
2
3
4
5
6
7
8
9
Queue size: 10
Getting integer data from the queue:
0 9
1 8
2 7
3 6
4 5
5 4
6 3
7 2
8 1
9 0
Notice how the dict data from the queue is always that same. It's always the last data put on the queue. The integer data behaves as I'd expect. Strings do as well, although I don't show it here.
What's going on here?