I have the following code, (contrived example):
# !/usr/bin/python
from multiprocessing import Queue
import threading
import Queue as Q
import time
from random import randint
class SomeClass:
def takes_awhile(self, mq):
qjson = {}
time.sleep(5)
qjson.update(
{"randint": randint(1, 9)}
)
mq.put(qjson)
def record(self, jq):
while True:
self.takes_awhile(jq)
time.sleep(0.05)
sc = SomeClass
jq = Queue()
scp = threading.Thread(target=sc.record, args=(jq,))
scp.start()
def qprint():
try:
rv = jq.get_nowait()
except Q.Empty:
return "Empty"
return rv
while True:
print qprint()
What I want this to do is for qprint() to always return immediately when called, printing Empty if too little time elapsed for takes_awhile to have put anything into the queue, but after about 5 seconds or so, I should start seeing the return value of takes_awhile (json with a random number), being pulled out of the queue. Instead, no matter how long the loop runs, it's always printing Empty.
What am I overlooking here? Any help is much appreciated.