def receive(self, filter_fn: Callable):
""" Apply filter to all messages in the inbox, first message for which
filter returns True will be returned.
:param filter_fn: A callable which checks if message is desired
(and returns True) or should be skipped (and returns False)
:returns: Message, if the filter returned True, otherwise ``None``
if no message matches or the mailbox was empty
"""
if self.queue_.empty():
return None
# try every element in the queue, get it, check it, place it into the
# queue end (NOTE: This will mix the messages breaking the order)
try:
for i in range(len(self.queue_)):
m = self.queue_.get_nowait()
if filter_fn(m):
LOG("Mailbox: match return", m)
return m
self.queue_.put(m)
except queue.Empty:
pass
return None
评论列表
文章目录