GLib.ThreadPool¶
Fields¶
Name |
Type |
Access |
Description |
---|---|---|---|
exclusive |
r/w |
are all threads exclusive to this pool |
|
func |
r/w |
the function to execute in the threads of this pool |
|
user_data |
r/w |
the user data for the threads of this pool |
Methods¶
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
|
|
|
|
|
|
|
|
|
Details¶
- class GLib.ThreadPool¶
The
GThreadPool
struct represents a thread pool.A thread pool is useful when you wish to asynchronously fork out the execution of work and continue working in your own thread. If that will happen often, the overhead of starting and destroying a thread each time might be too high. In such cases reusing already started threads seems like a good idea. And it indeed is, but implementing this can be tedious and error-prone.
Therefore GLib provides thread pools for your convenience. An added advantage is, that the threads can be shared between the different subsystems of your program, when they are using GLib.
To create a new thread pool, you use [func`GLib`.ThreadPool.new]. It is destroyed by [method`GLib`.ThreadPool.free].
If you want to execute a certain task within a thread pool, use [method`GLib`.ThreadPool.push].
To get the current number of running threads you call [method`GLib`.ThreadPool.get_num_threads]. To get the number of still unprocessed tasks you call [method`GLib`.ThreadPool.unprocessed]. To control the maximum number of threads for a thread pool, you use [method`GLib`.ThreadPool.get_max_threads]. and [method`GLib`.ThreadPool.set_max_threads].
Finally you can control the number of unused threads, that are kept alive by GLib for future use. The current number can be fetched with [func`GLib`.ThreadPool.get_num_unused_threads]. The maximum number can be controlled by [func`GLib`.ThreadPool.get_max_unused_threads] and [func`GLib`.ThreadPool.set_max_unused_threads]. All currently unused threads can be stopped by calling [func`GLib`.ThreadPool.stop_unused_threads].
- classmethod get_max_idle_time()[source]¶
- Returns:
the maximum interval (milliseconds) to wait for new tasks in the thread pool before stopping the thread
- Return type:
This function will return the maximum interval that a thread will wait in the thread pool for new tasks before being stopped.
If this function returns 0, threads waiting in the thread pool for new work are not stopped.
New in version 2.10.
- classmethod get_max_unused_threads()[source]¶
- Returns:
the maximal number of unused threads
- Return type:
Returns the maximal allowed number of unused threads.
- classmethod get_num_unused_threads()[source]¶
- Returns:
the number of currently unused threads
- Return type:
Returns the number of currently unused threads.
- classmethod set_max_idle_time(interval)[source]¶
- Parameters:
interval (
int
) – the maximum interval (in milliseconds) a thread can be idle
This function will set the maximum interval that a thread waiting in the pool for new tasks can be idle for before being stopped. This function is similar to calling
GLib.ThreadPool.stop_unused_threads
() on a regular timeout, except this is done on a per thread basis.By setting interval to 0, idle threads will not be stopped.
The default value is 15000 (15 seconds).
New in version 2.10.
- classmethod set_max_unused_threads(max_threads)[source]¶
- Parameters:
max_threads (
int
) – maximal number of unused threads
Sets the maximal number of unused threads to max_threads. If max_threads is -1, no limit is imposed on the number of unused threads.
The default value is 2.
- classmethod stop_unused_threads()[source]¶
Stops all currently unused threads. This does not change the maximal number of unused threads. This function can be used to regularly stop all unused threads e.g. from
GLib.timeout_add
().
- free(immediate, wait_)[source]¶
- Parameters:
Frees all resources allocated for self.
If immediate is
True
, no new task is processed for self. Otherwise self is not freed before the last task is processed. Note however, that no thread of this pool is interrupted while processing a task. Instead at least all still running threads can finish their tasks before the self is freed.If wait_ is
True
, this function does not return before all tasks to be processed (dependent on immediate, whether all or only the currently running) are ready. Otherwise this function returns immediately.After calling this function self must not be used anymore.
- get_max_threads()[source]¶
- Returns:
the maximal number of threads
- Return type:
Returns the maximal number of threads for self.
- get_num_threads()[source]¶
- Returns:
the number of threads currently running
- Return type:
Returns the number of threads currently running in self.
- move_to_front(data)[source]¶
- Parameters:
- Returns:
True
if the item was found and moved- Return type:
Moves the item to the front of the queue of unprocessed items, so that it will be processed next.
New in version 2.46.
- push(data)[source]¶
- Parameters:
- Raises:
- Returns:
- Return type:
Inserts data into the list of tasks to be executed by self.
When the number of currently running threads is lower than the maximal allowed number of threads, a new thread is started (or reused) with the properties given to g_thread_pool_new(). Otherwise, data stays in the queue until a thread in this pool finishes its previous task and processes data.
error can be
None
to ignore errors, or non-None
to report errors. An error can only occur when a new thread couldn’t be created. In that case data is simply appended to the queue of work to do.Before version 2.32, this function did not return a success status.
- set_max_threads(max_threads)[source]¶
- Parameters:
max_threads (
int
) – a new maximal number of threads for self, or -1 for unlimited- Raises:
- Returns:
- Return type:
Sets the maximal allowed number of threads for self. A value of -1 means that the maximal number of threads is unlimited. If self is an exclusive thread pool, setting the maximal number of threads to -1 is not allowed.
Setting max_threads to 0 means stopping all work for self. It is effectively frozen until max_threads is set to a non-zero value again.
A thread is never terminated while calling func, as supplied by g_thread_pool_new(). Instead the maximal number of threads only has effect for the allocation of new threads in
GLib.ThreadPool.push
(). A new thread is allocated, whenever the number of currently running threads in self is smaller than the maximal number.error can be
None
to ignore errors, or non-None
to report errors. An error can only occur when a new thread couldn’t be created.Before version 2.32, this function did not return a success status.