Use get_nowait() in python without raising empty exception

willpower2727

I have an inter-process queue that is usually empty and only once in a while does something appear in it. In one of my threads I want to define a while loop like this:

def mythread(queue1):

    while (queue1.get_nowait() != 1):
        #do stuff

This works great until the queue is empty which happens quickly in my case. When the queue is empty calling get_nowait() or get(False) raises the empty queue exception. Is there any way to check the queue without blocking AND without raising the empty exception?

Jean-François Fabre

use empty method

if queue1.empty():
   pass
else:
   # get items from the queue

Note: doc says "not reliable" (no kidding).

I suppose that's because it can return True (queue is empty) while a message is posted, so the operation is not as atomic as catching the exception. Well, followed by a get_nowait() call I suppose it doesn't matter unless some other thread can consume the queue as well, in which case a racing condition could occur and the queue could be empty when you try to read from it!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Manually raising (throwing) an exception in Python

From Dev

How to get stack trace string without raising exception in python?

From Dev

NSTextView, setString: raising exception

From Dev

Showing a empty GET form without raising any error on Django

From Dev

Python copy.deepcopy() fails without raising warning, exception or error

From Dev

Context manager that returns from parent function without raising an exception

From Dev

Python Argument Parser, Raising an exception before -h

From Dev

Python exception handling and raising

From Dev

Raising a custom message for exception in function

From Dev

raising an Exception in an Except without calling the original Exception

From Dev

Python not raising an exception on % without conversion specifier

From Dev

raising an exception in else part of a for loop in Python

From Dev

Call to unmanaged dll kills process without raising exception

From Dev

How to use GADTs across modules in OCaml without raising warnings?

From Dev

Raising non-Exception based exceptions in Python gives different results

From Dev

Using Rails' active record, is there another way to get a rollback without raising an exception?

From Dev

How to loop over multiple files, ignore empty files without raising an exception?

From Dev

Python - Parameter checking with Exception Raising

From Dev

Use get_nowait() in python without raising empty exception

From Dev

Django: How to rollback (@transaction.atomic) without raising exception?

From Dev

How to get stack trace string without raising exception in python?

From Dev

java substring raising an exception

From Dev

Python empty variable exception

From Dev

Why is this SQL INSERT failing without raising and exception?

From Dev

Raising non-Exception based exceptions in Python gives different results

From Dev

Parsing currency to double raising exception

From Dev

Raising exception in postgresql

From Dev

How do I exit a for loop in python 2.7 without raising Exceptions

From Dev

How to use a range variable in a list without raising a SyntaxError?

Related Related

  1. 1

    Manually raising (throwing) an exception in Python

  2. 2

    How to get stack trace string without raising exception in python?

  3. 3

    NSTextView, setString: raising exception

  4. 4

    Showing a empty GET form without raising any error on Django

  5. 5

    Python copy.deepcopy() fails without raising warning, exception or error

  6. 6

    Context manager that returns from parent function without raising an exception

  7. 7

    Python Argument Parser, Raising an exception before -h

  8. 8

    Python exception handling and raising

  9. 9

    Raising a custom message for exception in function

  10. 10

    raising an Exception in an Except without calling the original Exception

  11. 11

    Python not raising an exception on % without conversion specifier

  12. 12

    raising an exception in else part of a for loop in Python

  13. 13

    Call to unmanaged dll kills process without raising exception

  14. 14

    How to use GADTs across modules in OCaml without raising warnings?

  15. 15

    Raising non-Exception based exceptions in Python gives different results

  16. 16

    Using Rails' active record, is there another way to get a rollback without raising an exception?

  17. 17

    How to loop over multiple files, ignore empty files without raising an exception?

  18. 18

    Python - Parameter checking with Exception Raising

  19. 19

    Use get_nowait() in python without raising empty exception

  20. 20

    Django: How to rollback (@transaction.atomic) without raising exception?

  21. 21

    How to get stack trace string without raising exception in python?

  22. 22

    java substring raising an exception

  23. 23

    Python empty variable exception

  24. 24

    Why is this SQL INSERT failing without raising and exception?

  25. 25

    Raising non-Exception based exceptions in Python gives different results

  26. 26

    Parsing currency to double raising exception

  27. 27

    Raising exception in postgresql

  28. 28

    How do I exit a for loop in python 2.7 without raising Exceptions

  29. 29

    How to use a range variable in a list without raising a SyntaxError?

HotTag

Archive