Sam
Sam

Reputation: 467

Can you get and put into a Queue at the same time?

I currently have a program that starts a thread with a FIFO queue. The queue is constantly putting data into the queue, and there is a method to get the items from the queue. The method acquires a lock and releases it once it grabs the items.

My question is, will I face into any problems in the future putting and getting from the queue simultaneously? Would I need to add a lock when putting data into the queue?

Thanks.

Upvotes: 0

Views: 1407

Answers (1)

masnun
masnun

Reputation: 11906

The Queue type has blocking calls to get() and put() by default. So when you make a get() call, it will block the call and wait for an item to be put in the queue.

The put() call will also by default block if the queue is full and wait for a slot to be free before it can put the item.

This default behaviour might be altered by using block=False or passing a positive integer to timeout. If you disable blocking or set a timeout, the call will try to execute normally and if it fails (within the timeout), it will raise certain exceptions.

Disabling blocking will fail instantly where as setting a timeout value would fail after those seconds.

Since the default nature of the calls are blocking, you should not run into any issues. Even if you disable blocking, still there will be exceptions which you can handle and properly control the flow of the program. So there should not be an issue from simultaneously accessing the queue as it is "synchronized".

Upvotes: 1

Related Questions