0

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.

2 Answers 2

1

You have a few minor errors. You need to instantiate SomeClass with sc = SomeClass(). You're missing the parentheses. It would also be a good idea to put a small sleep in your while loop. With this printing so fast, you can't see the non 'Empty' statements. Here's your revised code...

#!/usr/bin/python
from multiprocessing import Queue
import threading
import Queue as Q
import time
from random import randint

class SomeClass(object):
    def takes_awhile(self, mq):
        qjson = {}
        time.sleep(1)
        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()
    time.sleep(0.25)
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is currently invalid - I get an exception like

TypeError: unbound method record() must be called with SomeClass instance as first argument (got Queue instance instead)

When passing parameters to sc.record inside the Thread call, you aren't referencing an instance of the SomeClass but instead the class itself - and so the self parameter isn't passed correctly. To resolve this change sc = SomeClass() to instantiate the class, and then the threading.Thread call passes an object.

I added a while loop so it runs indefinitely and prints results. After these changes it seems to work perfectly well for me.

Output looks like this:

Empty 
Empty 
Empty 
Empty 
{'randint': 4} 
Empty 
Empty

Here's the code I ran:

from multiprocessing import Queue
import threading
import time
import Queue as Q
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:
    time.sleep(1)    
    print qprint()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.