Task#
Superclasses: Object
, InitiallyUnowned
, Object
Task
is used by Element
and Pad
to provide the data passing
threads in a Pipeline
.
A Pad
will typically start a Task
to push or pull data to/from the
peer pads. Most source elements start a Task
to push data. In some cases
a demuxer element can start a Task
to pull data from a peer element. This
is typically done when the demuxer can perform random access on the upstream
peer element for improved performance.
Although convenience functions exist on Pad
to start/pause/stop tasks, it
might sometimes be needed to create a Task
manually if it is not related to
a Pad
.
Before the Task
can be run, it needs a RecMutex
that can be set with
set_lock()
.
The task can be started, paused and stopped with start()
, pause()
and stop()
respectively or with the set_state()
function.
A Task
will repeatedly call the TaskFunction
with the user data
that was provided when creating the task with new()
. While calling
the function it will acquire the provided lock. The provided lock is released
when the task pauses or stops.
Stopping a task with stop()
will not immediately make sure the task is
not running anymore. Use join()
to make sure the task is completely
stopped and the thread is stopped.
After creating a Task
, use unref()
to free its resources. This can
only be done when the task is not running anymore.
Task functions can send a Message
to send out-of-band data to the
application. The application can receive messages from the Bus
in its
mainloop.
For debugging purposes, the task will configure its object name as the thread name on Linux. Please note that the object name should be configured before the task is started; changing the object name after the task has been started, has no effect on the thread name.
Constructors#
- class Task
- classmethod new(func: Callable[[Any], None], user_data: Any = None) Task #
Create a new Task that will repeatedly call the provided
func
withuser_data
as a parameter. Typically the task will run in a new thread.The function cannot be changed after the task has been created. You must create a new
Task
to change the function.This function will not yet create and start a thread. Use
start()
orpause()
to create and start the GThread.Before the task can be used, a
RecMutex
must be configured using theset_lock()
function. This lock will always be acquired whilefunc
is called.- Parameters:
func – The
TaskFunction
to useuser_data – User data to pass to
func
Methods#
- class Task
- classmethod cleanup_all() None #
Wait for all tasks to be stopped. This is mainly used internally to ensure proper cleanup of internal data structures in test suites.
MT safe.
- join() bool #
Joins
task
. After this call, it is safe to unref the task and clean up the lock set withset_lock()
.The task will automatically be stopped with this call.
This function cannot be called from within a task function as this would cause a deadlock. The function will detect this and print a g_warning.
- pause() bool #
Pauses
task
. This method can also be called on a task in the stopped state, in which case a thread will be started and will remain in the paused state. This function does not wait for the task to complete the paused state.
- resume() bool #
Resume
task
in case it was paused. If the task was stopped, it will remain in that state and this function will returnFalse
.Added in version 1.18.
- set_enter_callback(enter_func: Callable[[Task, Thread, Any], None], user_data: Any = None) None #
Call
enter_func
when the task function oftask
is entered.user_data
will be passed toenter_func
andnotify
will be called whenuser_data
is no longer referenced.- Parameters:
enter_func – a
TaskThreadFunc
user_data – user data passed to
enter_func
- set_leave_callback(leave_func: Callable[[Task, Thread, Any], None], user_data: Any = None) None #
Call
leave_func
when the task function oftask
is left.user_data
will be passed toleave_func
andnotify
will be called whenuser_data
is no longer referenced.- Parameters:
leave_func – a
TaskThreadFunc
user_data – user data passed to
leave_func
- set_lock(mutex: RecMutex) None #
Set the mutex used by the task. The mutex will be acquired before calling the
TaskFunction
.This function has to be called before calling
pause()
orstart()
.MT safe.
- Parameters:
mutex – The
RecMutex
to use
- set_pool(pool: TaskPool) None #
Set
pool
as the new GstTaskPool fortask
. Any new streaming threads that will be created bytask
will now usepool
.MT safe.
- Parameters:
pool – a
TaskPool
- set_state(state: TaskState) bool #
Sets the state of
task
tostate
.The
task
must have a lock associated with it usingset_lock()
when going to GST_TASK_STARTED or GST_TASK_PAUSED or this function will returnFalse
.MT safe.
- Parameters:
state – the new task state
- start() bool #
Starts
task
. Thetask
must have a lock associated with it usingset_lock()
or this function will returnFalse
.
Fields#
- class Task
- cond#
Used to pause/resume the task
- func#
The function executed by this task
- lock#
The lock taken when iterating the task function
- notify#
GDestroyNotify for
user_data
- object#
- priv#
- running#
A flag indicating that the task is running
- state#
The state of the task
- thread#
- user_data#
User_data passed to the task function