Threaded or synchronous Dispatch Queues for Godot
Warning
This branch is only usable with Godot 4.
For Godot 3 support, check out the godot-3 branch.
Threaded or synchronous Dispatch Queues for Godot.
Threaded Dispatch Queues are also known as Thread Pools.
Available at the Asset Library as Dispatch Queue.
# 1) Instantiate
var dispatch_queue = DispatchQueue.new()
# 2.a) Either create a serial...
dispatch_queue.create_serial()
# 2.b) ...or concurrent queue
dispatch_queue.create_concurrent(OS.get_processor_count())
# (if you do neither, DispatchQueue will run in synchronous mode)
# 3) Dispatch methods, optionally responding to tasks and task groups "finished" signal
# 3.a) Fire and forget style
dispatch_queue.dispatch(self.method_name.bind("optional", "method", "arguments")).then(self.result_callback)
dispatch_queue.dispatch_group([
self.method_name1.bind("optional", "arguments"),
self.method_name2,
self.method_name3,
]).then_deferred(self.group_results_callback)
# 3.b) Coroutine style
var task = dispatch_queue.dispatch(self.mymethod)
var mymethod_result = await task.finished
var task_group = dispatch_queue.dispatch_group([self.method1, self.method2])
var group_method_results = await task_group.finished
# 4) Optionally respond to the `all_tasks_finished` signal to know when all tasks have finished
# 4.a) Connect style
dispatch_queue.all_tasks_finished.connect(self._on_all_tasks_finished)
# 4.b) Coroutine style
await dispatch_queue.all_tasks_finished
# DispatchQueue extends RefCounted, so no need to worry about freeing it manually
There is a Node script (addons/dispatch_queue/dispatch_queue_node.gd)
that wraps every aspect of dispatch queues. Useful for having a local queue in a scene or as an Autoload.
There is also a Resource script (addons/dispatch_queue/dispatch_queue_resource.gd)
that wraps every aspect of dispatch queues. Useful for sharing queues with multiple objects between scenes without resorting to Autoload.
signal all_tasks_finished()
create_serial()
shutdown
and create a new Thread.create_concurrent(thread_count: int = 1)
thread_count
Threads of execution to process tasks.thread_count
Threads,shutdown
and create new Threads.thread_count <= 1
, creates a serial queue.dispatch(callable: Callable, priority: int = 0) -> Task
callable
, optionally setting a priority
priority
order.priority
order on the next frame.priority
is lower will execute first.dispatch_group(task_list: Array[Callable], priority: int = 0) -> TaskGroup
task_list
by calling dispatch
on each value with priority priority
, returning the TaskGroup associated with them.priority
is lower will execute first.is_threaded() -> bool
get_thread_count() -> int
size() -> int
is_empty() -> bool
clear()
shutdown()
create_serial
or create_concurrent
to recreate the worker threads.NOTIFICATION_PREDELETE
.signal finished(result)
CONNECT_DEFERRED
if you want to call non Thread-safethen(callable: Callable, flags: int = 0)
dispatch_queue.dispatch(task).then(continuation_callable)
then_deferred(callable: Callable, flags: int = 0)
then
that also adds CONNECT_DEFERRED
to flags.
dispatch_queue.dispatch(task).then_deferred(continuation_callable)
signal finished(results)
CONNECT_DEFERRED
if you want to call non Thread-safethen(callable: Callable, flags: int = 0)
dispatch_queue.dispatch_group(task_list).then(continuation_callable)
then_deferred(callable: Callable, flags: int = 0)
then
that also adds CONNECT_DEFERRED
to flags.
dispatch_queue.dispatch_group(task_list).then_deferred(continuation_callable)
Node that wraps a DispatchQueue.
Apart from creation, all DispatchQueue public methods and signals are supported.
Creates the Threads when entering tree and shuts down when exiting tree.
export(int) var thread_count = -1
thread_count == 0
, runs queue in synchronous mode.thread_count < 0
, creates OS.get_processor_count()
Threads.Resource that wraps a DispatchQueue.
Apart from creation, all DispatchQueue public methods and signals are supported.
export(int) var thread_count = -1
thread_count == 0
, runs queue in synchronous mode.thread_count < 0
, creates OS.get_processor_count()
Threads.