Thursday, March 6, 2014

Does Queue.get block main?

Yes -- if you call some_queue.get() within either the thread or the main function, the program will block there until some object as passed through the queue.

However, it is possible to use queues so that they don't block, or so that they have a timeout of some kind:
import Queue

while True:
    try:
        data = some_queue.get(False) 
        # If `False`, the program is not blocked. `Queue.Empty` is thrown if
        # the queue is empty
    except Queue.Empty:
        data = None

    try:
        data2 = some_queue.get(True, 3) 
        # Waits for 3 seconds, otherwise throws `Queue.Empty`
    except Queue.Empty:
        data = None

You can do the same for some_queue.put -- either do some_queue.put(item, False) for non-blocking queues, or some_queue.put(item, True, 3) for timeouts. If your queue has a size limit, it will throw a Queue.Full exception if there is no more room left to append a new item.

No comments:

My Profile

My photo
can be reached at 09916017317