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. If queue is empty, this function blocks until data becomes available.

pop_unlocked() None

Pops data from the queue. If queue 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 the queue.

The data parameter must not be None.

Parameters:

data – data to push onto the queue

push_front(item: None) None

Pushes the item into the queue. item must not be None. In contrast to push(), 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 the queue. item must not be None. In contrast to push_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 into queue using func to determine the new position.

This function requires that the queue is sorted before pushing on new elements, see sort().

This function will lock queue before it sorts the queue and unlock it when it is finished.

For an example of func see sort().

Added in version 2.10.

Parameters:
  • data – the data to push into the queue

  • func – the CompareDataFunc is used to sort queue

  • user_data – user data passed to func.

push_sorted_unlocked(data: None, func: Callable[[...], int], *user_data: Any) None

Inserts data into queue using func to determine the new position.

The sort function func is passed two elements of the queue. It should return 0 if they are equal, a negative value if the first element should be higher in the queue or a positive value if the first element should be lower in the queue than the second element.

This function requires that the queue is sorted before pushing on new elements, see sort().

This function must be called while holding the queue’s lock.

For an example of func see sort().

Added in version 2.10.

Parameters:
  • data – the data to push into the queue

  • func – the CompareDataFunc is used to sort queue

  • user_data – user data passed to func.

push_unlocked(data: None) None

Pushes the data into the queue.

The data parameter must not be None.

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 using func.

The sort function func is passed two elements of the queue. It should return 0 if they are equal, a negative value if the first element should be higher in the queue or a positive value if the first element should be lower in the queue 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 sort queue

  • user_data – user data passed to func

sort_unlocked(func: Callable[[...], int], *user_data: Any) None

Sorts queue using func.

The sort function func is passed two elements of the queue. It should return 0 if they are equal, a negative value if the first element should be higher in the queue or a positive value if the first element should be lower in the queue 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 sort queue

  • user_data – user data passed to func

timed_pop(end_time: TimeVal) None

Pops data from the queue. If the queue is empty, blocks until end_time or until data becomes available.

If no data is received before end_time, None is returned.

To easily calculate end_time, a combination of get_real_time() and add() 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 until end_time or until data becomes available.

If no data is received before end_time, None is returned.

To easily calculate end_time, a combination of get_real_time() and add() 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 for timeout 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 for timeout 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

try_pop() None

Tries to pop data from the queue. If no data is available, None is returned.

try_pop_unlocked() None

Tries to pop data from the queue. If no data is available, None is returned.

This function must be called while holding the queue’s lock.

unlock() None

Releases the queue’s lock.

Calling this function when you have not acquired the with lock() leads to undefined behaviour.