AsyncQueue
- class AsyncQueue(*args, **kwargs)
An opaque data structure which represents an asynchronous queue.
It should only be accessed through the ``g_async_queue_``*
functions.
Methods
- class AsyncQueue
- length() int
Returns the length of the queue.
Actually this function returns the number of data items in the queue minus the number of waiting threads, so a negative value means waiting threads, and a positive value means available entries in the
queue
. A return value of 0 could mean n entries in the queue and n threads waiting. This can happen due to locking of the queue or due to scheduling.
- length_unlocked() int
Returns the length of the queue.
Actually this function returns the number of data items in the queue minus the number of waiting threads, so a negative value means waiting threads, and a positive value means available entries in the
queue
. A return value of 0 could mean n entries in the queue and n threads waiting. This can happen due to locking of the queue or due to scheduling.This function must be called while holding the
queue
’s lock.
- lock() None
Acquires the
queue
’s lock. If another thread is already holding the lock, this call will block until the lock becomes available.Call
unlock()
to drop the lock again.While holding the lock, you can only call the
g_async_queue_``*_unlocked() functions on ``queue
. Otherwise, deadlock may occur.
- pop() None
Pops data from the
queue
. Ifqueue
is empty, this function blocks until data becomes available.
- pop_unlocked() None
Pops data from the
queue
. Ifqueue
is empty, this function blocks until data becomes available.This function must be called while holding the
queue
’s lock.
- push(data: None) None
Pushes the
data
into thequeue
.The
data
parameter must not beNone
.- Parameters:
data – data to push onto the
queue
- push_front(item: None) None
Pushes the
item
into thequeue
.item
must not beNone
. In contrast topush()
, this function pushes the new item ahead of the items already in the queue, so that it will be the next one to be popped off the queue.Added in version 2.46.
- Parameters:
item – data to push into the
queue
- push_front_unlocked(item: None) None
Pushes the
item
into thequeue
.item
must not beNone
. In contrast topush_unlocked()
, this function pushes the new item ahead of the items already in the queue, so that it will be the next one to be popped off the queue.This function must be called while holding the
queue
’s lock.Added in version 2.46.
- Parameters:
item – data to push into the
queue
- push_sorted(data: None, func: Callable[[...], int], *user_data: Any) None
Inserts
data
intoqueue
usingfunc
to determine the new position.This function requires that the
queue
is sorted before pushing on new elements, seesort()
.This function will lock
queue
before it sorts the queue and unlock it when it is finished.For an example of
func
seesort()
.Added in version 2.10.
- Parameters:
data – the
data
to push into thequeue
func – the
CompareDataFunc
is used to sortqueue
user_data – user data passed to
func
.
- push_sorted_unlocked(data: None, func: Callable[[...], int], *user_data: Any) None
Inserts
data
intoqueue
usingfunc
to determine the new position.The sort function
func
is passed two elements of thequeue
. It should return 0 if they are equal, a negative value if the first element should be higher in thequeue
or a positive value if the first element should be lower in thequeue
than the second element.This function requires that the
queue
is sorted before pushing on new elements, seesort()
.This function must be called while holding the
queue
’s lock.For an example of
func
seesort()
.Added in version 2.10.
- Parameters:
data – the data to push into the
queue
func – the
CompareDataFunc
is used to sortqueue
user_data – user data passed to
func
.
- push_unlocked(data: None) None
Pushes the
data
into thequeue
.The
data
parameter must not beNone
.This function must be called while holding the
queue
’s lock.- Parameters:
data – data to push onto the
queue
- remove(item: None) bool
Remove an item from the queue.
Added in version 2.46.
- Parameters:
item – the data to remove from the
queue
- Returns:
0 if the file was successfully removed, -1 if an error occurred
- remove_unlocked(item: None) bool
Remove an item from the queue.
This function must be called while holding the
queue
’s lock.Added in version 2.46.
- Parameters:
item – the data to remove from the
queue
- sort(func: Callable[[...], int], *user_data: Any) None
Sorts
queue
usingfunc
.The sort function
func
is passed two elements of thequeue
. It should return 0 if they are equal, a negative value if the first element should be higher in thequeue
or a positive value if the first element should be lower in thequeue
than the second element.This function will lock
queue
before it sorts the queue and unlock it when it is finished.If you were sorting a list of priority numbers to make sure the lowest priority would be at the top of the queue, you could use:
gint32 id1; gint32 id2; id1 = GPOINTER_TO_INT (element1); id2 = GPOINTER_TO_INT (element2); return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
Added in version 2.10.
- Parameters:
func – the
CompareDataFunc
is used to sortqueue
user_data – user data passed to
func
- sort_unlocked(func: Callable[[...], int], *user_data: Any) None
Sorts
queue
usingfunc
.The sort function
func
is passed two elements of thequeue
. It should return 0 if they are equal, a negative value if the first element should be higher in thequeue
or a positive value if the first element should be lower in thequeue
than the second element.This function must be called while holding the
queue
’s lock.Added in version 2.10.
- Parameters:
func – the
CompareDataFunc
is used to sortqueue
user_data – user data passed to
func
- timed_pop(end_time: TimeVal) None
Pops data from the
queue
. If the queue is empty, blocks untilend_time
or until data becomes available.If no data is received before
end_time
,None
is returned.To easily calculate
end_time
, a combination ofget_real_time()
andadd()
can be used.Deprecated since version Unknown: use
timeout_pop()
.- Parameters:
end_time – a
TimeVal
, determining the final time
- timed_pop_unlocked(end_time: TimeVal) None
Pops data from the
queue
. If the queue is empty, blocks untilend_time
or until data becomes available.If no data is received before
end_time
,None
is returned.To easily calculate
end_time
, a combination ofget_real_time()
andadd()
can be used.This function must be called while holding the
queue
’s lock.Deprecated since version Unknown: use
timeout_pop_unlocked()
.- Parameters:
end_time – a
TimeVal
, determining the final time
- timeout_pop(timeout: int) None
Pops data from the
queue
. If the queue is empty, blocks fortimeout
microseconds, or until data becomes available.If no data is received before the timeout,
None
is returned.- Parameters:
timeout – the number of microseconds to wait
- timeout_pop_unlocked(timeout: int) None
Pops data from the
queue
. If the queue is empty, blocks fortimeout
microseconds, or until data becomes available.If no data is received before the timeout,
None
is returned.This function must be called while holding the
queue
’s lock.- Parameters:
timeout – the number of microseconds to wait