How to get Python Queue.empty () multiprocessor process to return the correct value? Or alternatives?

I have this snippet that uses a class Queuefrom a module multiprocess. I am very confused that the .empty()instance method Queuedoes not give me the correct value as I would expect. This is my code:

from time import sleep
from multiprocessing import Queue, Lock

foo = Queue()
locker = Lock()

with locker:  # even with this, still True
    foo.put("bar")

print(foo.empty())  # True, obviously not
print(foo.empty())  # True
print(foo.empty())  # True
print(foo.qsize())  # 1L
print(foo.empty())  # True

However, if I use the function sleepfrom time, as this causes a chronological delay in execution. He works.

from time import sleep
from multiprocessing import Queue, Lock

foo = Queue()
locker = Lock()

foo.put("bar")

sleep(0.01)

print(foo.empty())  # False
print(foo.empty())  # False
print(foo.empty())  # False
print(foo.qsize())  # 1L
print(foo.empty())  # False

I know that my alternative is an expression .qsize() > 0, but I'm sure I'm just doing it wrong.

What am I doing wrong?

* EDIT *

Now I understand that this is unreliable, thanks @Mathias Ettinger. Any clean alternatives? I need to know that I need to know reliably whether mine is empty Queueor not.

+4
2

, empty(), full(), qsize() .

:

  • , Queue:

    AMT = 8
    for _ in range(AMT):
        queue.put('some stuff')
    
    for _ in range(AMT):
        print(queue.get())
    

    , , .

  • :

    num_threads = 8
    guardian = 'STUFF DONE'
    
    while num_threads:
        item = queue.get()
        if item == guardian:
            num_threads -= 1
        else:
            process(item)
    

    , ( ), , .

+5

, Queue, , .empty() , .qsize().

.qsize() ( ), .empty() .qsize(), :

# mp.Queue() is a function, not a class, so we need to find the true class
# to subclass
import multiprocessing.queues

class XQueue(multiprocessing.queues.Queue):
    def empty(self):
        try:
            return self.qsize() == 0
        except NotImplementedError:  # OS X -- see qsize() implementation
            return super(XQueue, self).empty()

.put() interprocess, . ( .get(), .) , : , -, .empty().

, . , , (empty/full/count) "", , , , , , . ( , python Queue.Queue, , "", .) .empty() .

+5

Source: https://habr.com/ru/post/1605919/


All Articles