GLib.AsyncQueue¶
record (struct)
An opaque data structure which represents an asynchronous queue.
It should only be accessed through the g_async_queue_* functions.
Methods¶
length¶
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¶
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¶
Acquires the queue's lock. If another thread is already
holding the lock, this call will block until the lock
becomes available.
Call AsyncQueue.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¶
Pops data from the queue. If queue is empty, this function
blocks until data becomes available.
pop_unlocked¶
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¶
Pushes the data into the queue.
The data parameter must not be None.
Parameters:
data— data to push onto thequeue
push_front¶
Pushes the item into the queue. item must not be None.
In contrast to AsyncQueue.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.
Parameters:
item— data to push into thequeue
push_front_unlocked¶
Pushes the item into the queue. item must not be None.
In contrast to AsyncQueue.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.
Parameters:
item— data to push into thequeue
push_sorted¶
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 AsyncQueue.sort.
This function will lock queue before it sorts the queue and unlock
it when it is finished.
For an example of func see AsyncQueue.sort.
Parameters:
data— thedatato push into thequeuefunc— theGCompareDataFuncis used to sortqueue
push_sorted_unlocked¶
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 AsyncQueue.sort.
This function must be called while holding the queue's lock.
For an example of func see AsyncQueue.sort.
Parameters:
data— the data to push into thequeuefunc— theGCompareDataFuncis used to sortqueue
push_unlocked¶
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 thequeue
ref¶
Increases the reference count of the asynchronous queue by 1.
You do not need to hold the lock to call this function.
ref_unlocked¶
:::warning Deprecated since 2.8 This API is deprecated. :::
Increases the reference count of the asynchronous queue by 1.
remove¶
Remove an item from the queue.
Parameters:
item— the data to remove from thequeue
remove_unlocked¶
Remove an item from the queue.
This function must be called while holding the queue's lock.
Parameters:
item— the data to remove from thequeue
sort¶
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);
Parameters:
func— theGCompareDataFuncis used to sortqueue
sort_unlocked¶
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.
Parameters:
func— theGCompareDataFuncis used to sortqueue
timed_pop¶
:::warning Deprecated This API is deprecated. :::
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 TimeVal.add can be used.
Parameters:
end_time— aTimeVal, determining the final time
timed_pop_unlocked¶
:::warning Deprecated This API is deprecated. :::
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 TimeVal.add can be used.
This function must be called while holding the queue's lock.
Parameters:
end_time— aTimeVal, determining the final time
timeout_pop¶
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¶
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¶
Tries to pop data from the queue. If no data is available,
None is returned.
try_pop_unlocked¶
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¶
Releases the queue's lock.
Calling this function when you have not acquired
the with AsyncQueue.lock leads to undefined
behaviour.
unref¶
Decreases the reference count of the asynchronous queue by 1.
If the reference count went to 0, the queue will be destroyed
and the memory allocated will be freed. So you are not allowed
to use the queue afterwards, as it might have disappeared.
You do not need to hold the lock to call this function.
unref_and_unlock¶
:::warning Deprecated since 2.8 This API is deprecated. :::
Decreases the reference count of the asynchronous queue by 1
and releases the lock. This function must be called while holding
the queue's lock. If the reference count went to 0, the queue
will be destroyed and the memory allocated will be freed.
Static functions¶
new¶
Creates a new asynchronous queue.
new_full¶
Creates a new asynchronous queue and sets up a destroy notify function that is used to free any remaining queue items when the queue is destroyed after the final unref.
Parameters:
item_free_func— function to free queue elements