Python2: How to speed up Queue.get with timeout?

I am trying to write a fast event system for multiple streams using Queues. Each stream has stdin and waits for it to be full.

So the main part of my flow is this:

class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stdin = Queue.Queue()
        # Some code here

    def run(self):
        while True:
             try:
                 order = self.stdin.get(block=True,timeout=5)
                 if self.do(order) is False:
                     break
             except Queue.Empty:
                 pass

    def do(self,order):
        if order["txt"] == "quit":
            return False
        deltaTime = time.time() - order["time"]
        print("Message : "+str(order["txt"])+", DeltaTime : "+str(deltaTime))
        # Some code here

      

I would like to do fast multimedia operations, so I need a real reactive system, but when I do some kind of litle test I got 50ms of latent juste with this (on a 2.4GHz Intel i7 processor)

This is my test code:

thread = MyThread()
thread.start()
thread.stdin.put({"txt": "test1", "time": time.time()})
time.sleep(1)
thread.stdin.put({"txt": "test2", "time": time.time()})
time.sleep(1)
thread.stdin.put({"txt": "test3", "time": time.time()})
time.sleep(1)
thread.stdin.put({"txt": "quit", "time": time.time()})
thread.join()
print("END")

      

And finally I got this:

Message : test1, DeltaTime : 0.00104689598083
Message : test2, DeltaTime : 0.0151479244232
Message : test3, DeltaTime : 0.0292360782623
END

      

That's about 20ms, but with the whole program, that's a lot for a real-time application! Now when I try without timeout, this is much better:

Message : test1, DeltaTime : 6.69956207275e-05
Message : test2, DeltaTime : 5.22136688232e-05
Message : test3, DeltaTime : 5.79357147217e-05
END

      

Is there any solution to speed up the Queue.get queue while keeping the timeout?

Thank!

+3


source to share





All Articles