In [35]:
import asyncio
import functools
def update(self, new_item):
    priority_level = new_item[0]
    def matching_level(element, priority_level):
        return element[0]==priority_level
    try:
        match_generator = (index for index,element in enumerate(self._queue)
                           if matching_level(element, priority_level))
        matching_index = next(match_generator)
        self._queue[matching_index] = new_item
    except StopIteration:
        self.put_nowait(new_item)
asyncio.PriorityQueue.update = update
q = asyncio.PriorityQueue(maxsize = 15)

In [36]:
def some_func(a):
    print(a)
one = functools.partial(some_func, 1)
two = functools.partial(some_func, 2)
three = functools.partial(some_func, 3)

In [39]:
q.put_nowait((0, one))

In [37]:
q.put_nowait((random.uniform(1.01, 1.99),two))

In [41]:
q.put_nowait((random.uniform(1.01, 1.99),three))

In [45]:
q.get_nowait()


Out[45]:
(1.8020180397803611,
 functools.partial(<function some_func at 0x7f4c6d9c0048>, 2))

In [27]:



Out[27]:
<PriorityQueue at 0x7f4c6d9b9f28 maxsize=15 _queue=[(1, functools.partial(<function some_func at 0x7f4c6de60f28>, 3)), (2, functools.partial(<function some_func at 0x7f4c6de60f28>, 2)), (4, functools.partial(<function some_func at 0x7f4c6de60f28>, 1))]>

In [33]:
q.put_nowait((random.uniform(1.01, 1.99),one))

In [34]:
q


Out[34]:
<PriorityQueue at 0x7f4c6d9b9f28 maxsize=15 _queue=[(1, functools.partial(<function some_func at 0x7f4c6de60f28>, 3)), (1.1, functools.partial(<function some_func at 0x7f4c6de60f28>, 1)), (4, functools.partial(<function some_func at 0x7f4c6de60f28>, 1)), (2, functools.partial(<function some_func at 0x7f4c6de60f28>, 2)), (1.1883725737240785, functools.partial(<function some_func at 0x7f4c6de60f28>, 1))]>

In [32]:
import random
random.uniform(1.01, 1.99)


Out[32]:
1.8815485511948773

In [ ]: