Dex.ThreadPool

g Dex.Object Dex.Object Dex.ThreadPool Dex.ThreadPool Dex.Object->Dex.ThreadPool

Subclasses:

None

Methods

Inherited:

Dex.Object (2)

class

new (n_threads)

close (mode)

get_n_threads ()

submit (thread_name, thread_func, *user_data)

Virtual Methods

None

Fields

None

Class Details

class Dex.ThreadPool
Bases:

Dex.Object

Abstract:

No

DexThreadPool is a thread pool for managing native OS threads similar to [struct`GLib`.ThreadPool].

The threads managed by DexThreadPool do not contain a [class`Dex`.Scheduler] which means that you cannot await futures or schedule [class`Dex`.Block] from a worker thread.

Threads are created up-front from [ctor`Dex`.ThreadPool.new].

DexThreadPool primarily exists for situations where you are using blocking external libraries and want to avoid calling [func`Dex`.thread_spawn] without any sort of queuing or bounding on the permitted concurrency.

Added in version 1.2.

classmethod new(n_threads)
Parameters:

n_threads (int) – the number of threads to create

Returns:

a new DexThreadPool

Return type:

Dex.ThreadPool

Creates a fixed-size pool of reusable operating system threads.

Added in version 1.2.

close(mode)
Parameters:

mode (Dex.ThreadPoolShutdownMode) – shutdown policy for queued work

Returns:

a future that resolves when shutdown completes

Return type:

Dex.Future

Begins shutting down the pool and prevents new submissions.

get_n_threads()
Returns:

the number of threads in the pool

Return type:

int

Gets the fixed number of threads owned by the pool.

Added in version 1.2.

submit(thread_name, thread_func, *user_data)
Parameters:
  • thread_name (str or None) – the name to use for debugging the returned future

  • thread_func (Dex.ThreadFunc) – the function to run on a pooled thread

  • user_data (object or None) – closure data for thread_func

Returns:

a future that resolves when the work completes

Return type:

Dex.Future

Queues blocking work to run on one of the pool’s reusable threads.

The provided thread_name is applied to the returned future using dex_future_set_static_name() so that tracing and debugging tools can identify the work item. It does not rename the underlying OS worker thread.

Added in version 1.2.