1

I was studying this example

http://eli.thegreenplace.net/2011/12/27/python-threads-communication-and-stopping/

He used like this

while not self.stoprequest.isSet():
    try:
        dirname = self.dir_q.get(True, 0.05)
        ... # do work
    except Queue.Empty:
        continue

I did n't get why he used block element in dir_q.get(True)

i want to know how the program behave if i use

self.dir_q.get()

Docs say that if we don't give anything then if there is soomething in queue , it will get it othwise it will raise exception.

i think whats problem with that

what the block, and timeout is doing

2 Answers 2

1

self.dir_q.get() blocks until an item is available; therefore your program might not be able to react on self.stoprequest.set() in time if you remove the timeout.

Sign up to request clarification or add additional context in comments.

3 Comments

now i get it. so i means get will hang the system untill it gets the item and it wont' move the program in next step untill the item is available. if we give timeout , then it will block for that timeout and chek if item is there , othwise it will move to next step. am i right
@user196264097: on timeout it executes continue in the except clause and checks if self.stoprequest.is_set() in the while condition before attempting to get the next item. If there is no timeout; the while condition is not checked until an item is put in the queue. `
@ealfonso , sorry for that , after understanding the logic , i later understand that u both were same. but it was sebestian comments which first cleared my doubt , and i can't accept two answers
1

Read the documentation on the Queue.

Queue.get([block[, timeout]]) Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

When the 'block' argument (not 'element') is True, and the 'timeout' a positive number, the Queue will wait 'timeout' seconds until an element is available in the Queue. If the Queue was empty at the time Queue.get() was called, and no other thread Queue.put()'s an element onto the Queue within the specified 'timeout' (eg .05 secs), the Queue raises Empty. Otherwise it returns the first element that was Queue.put() by another thread.

4 Comments

i want to know what will ahppen to program , if i don't use block and timeout
What do you not understand about the documentation?
i undertand the use of it , but i don't understand how will program behave differently if i don't use it. i mean what will happen if program goes to exception straight way without waitiing
Ok, I think I understand your question. I will answer it with a question: What happens if the user sets 'stoprequest' while the queue is blocking on get() for an element that will never come?

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.