What happens if a Python object is unbound?

import queue, threading

def getFromQueue(myQueue):
    while True:
        print(myQueue.get())

def putInQueue(myQueue):
    while True:
        putString=... #code that provides values
        myQueue.put(putString)


for msg in msgList:
    myQueue=queue.Queue()
    threading.Thread( target=getFromQueue, args=(myQueue, ) ).start()
    threading.Thread( target=putInQueue, args=(myQueue, ) ).start()

My goal is to have each pair of (getFromQueue and putInQueue) threads in a loop to share one unique queue. When I reassign myQueue to a new Queue Object, what happens to the old Queue object? Does each thread still use the original queue object that was passed when I spawned the thread or do all the existing threads get updated to point the new/latest one and all share a single Queue Object?

Answer

Each instance of Queue gets two more references to it before the global myQueue is assigned a new value: the myQueue parameter to getFromQueue and the myQueue parameter to putInQueue. An object is only destroyed once the last reference to an object is unbound.

The only effect on the previous instance of Queue as the result of the new assignment myQueue = queue.Queue() is that its reference count will decrease from 3 to 2.

Once both threads executing the functions for a given instance of Queue have exited, then that instance can be collected. The first thread that exits will result in the reference count decreasing from 2 to 1 when the local myQueue goes out of scope; the second will result in the reference count decreasing from 1 to 0.