:right-sidebar: True Task =================================================================== .. currentmodule:: gi.repository.Gst .. class:: Task(**properties: ~typing.Any) :no-contents-entry: Superclasses: :class:`~gi.repository.Gst.Object`, :class:`~gi.repository.GObject.InitiallyUnowned`, :class:`~gi.repository.GObject.Object` :obj:`~gi.repository.Gst.Task` is used by :obj:`~gi.repository.Gst.Element` and :obj:`~gi.repository.Gst.Pad` to provide the data passing threads in a :obj:`~gi.repository.Gst.Pipeline`\. A :obj:`~gi.repository.Gst.Pad` will typically start a :obj:`~gi.repository.Gst.Task` to push or pull data to/from the peer pads. Most source elements start a :obj:`~gi.repository.Gst.Task` to push data. In some cases a demuxer element can start a :obj:`~gi.repository.Gst.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 :obj:`~gi.repository.Gst.Pad` to start/pause/stop tasks, it might sometimes be needed to create a :obj:`~gi.repository.Gst.Task` manually if it is not related to a :obj:`~gi.repository.Gst.Pad`\. Before the :obj:`~gi.repository.Gst.Task` can be run, it needs a :obj:`~gi.repository.GLib.RecMutex` that can be set with :func:`~gi.repository.Gst.Task.set_lock`. The task can be started, paused and stopped with :func:`~gi.repository.Gst.Task.start`, :func:`~gi.repository.Gst.Task.pause` and :func:`~gi.repository.Gst.Task.stop` respectively or with the :func:`~gi.repository.Gst.Task.set_state` function. A :obj:`~gi.repository.Gst.Task` will repeatedly call the :obj:`~gi.repository.Gst.TaskFunction` with the user data that was provided when creating the task with :func:`~gi.repository.Gst.Task.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 :func:`~gi.repository.Gst.Task.stop` will not immediately make sure the task is not running anymore. Use :func:`~gi.repository.Gst.Task.join` to make sure the task is completely stopped and the thread is stopped. After creating a :obj:`~gi.repository.Gst.Task`\, use :func:`~gi.repository.Gst.Object.unref` to free its resources. This can only be done when the task is not running anymore. Task functions can send a :obj:`~gi.repository.Gst.Message` to send out-of-band data to the application. The application can receive messages from the :obj:`~gi.repository.Gst.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 ------------ .. rst-class:: interim-class .. class:: Task :no-index: .. classmethod:: new(func: ~typing.Callable[[~typing.Any], None], user_data: ~typing.Any = None) -> ~gi.repository.Gst.Task Create a new Task that will repeatedly call the provided ``func`` with ``user_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 :obj:`~gi.repository.Gst.Task` to change the function. This function will not yet create and start a thread. Use :func:`~gi.repository.Gst.Task.start` or :func:`~gi.repository.Gst.Task.pause` to create and start the GThread. Before the task can be used, a :obj:`~gi.repository.GLib.RecMutex` must be configured using the :func:`~gi.repository.Gst.Task.set_lock` function. This lock will always be acquired while ``func`` is called. :param func: The :obj:`~gi.repository.Gst.TaskFunction` to use :param user_data: User data to pass to ``func`` Methods ------- .. rst-class:: interim-class .. class:: Task :no-index: .. 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. .. method:: get_pool() -> ~gi.repository.Gst.TaskPool Get the :obj:`~gi.repository.Gst.TaskPool` that this task will use for its streaming threads. MT safe. .. method:: get_state() -> ~gi.repository.Gst.TaskState Get the current state of the task. .. method:: join() -> bool Joins ``task``\. After this call, it is safe to unref the task and clean up the lock set with :func:`~gi.repository.Gst.Task.set_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. .. method:: 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. .. method:: resume() -> bool Resume ``task`` in case it was paused. If the task was stopped, it will remain in that state and this function will return :const:`False`. .. versionadded:: 1.18 .. method:: set_enter_callback(enter_func: ~typing.Callable[[~gi.repository.Gst.Task, ~gi.repository.GLib.Thread, ~typing.Any], None], user_data: ~typing.Any = None) -> None Call ``enter_func`` when the task function of ``task`` is entered. ``user_data`` will be passed to ``enter_func`` and ``notify`` will be called when ``user_data`` is no longer referenced. :param enter_func: a :obj:`~gi.repository.Gst.TaskThreadFunc` :param user_data: user data passed to ``enter_func`` .. method:: set_leave_callback(leave_func: ~typing.Callable[[~gi.repository.Gst.Task, ~gi.repository.GLib.Thread, ~typing.Any], None], user_data: ~typing.Any = None) -> None Call ``leave_func`` when the task function of ``task`` is left. ``user_data`` will be passed to ``leave_func`` and ``notify`` will be called when ``user_data`` is no longer referenced. :param leave_func: a :obj:`~gi.repository.Gst.TaskThreadFunc` :param user_data: user data passed to ``leave_func`` .. method:: set_lock(mutex: ~gi.repository.GLib.RecMutex) -> None Set the mutex used by the task. The mutex will be acquired before calling the :obj:`~gi.repository.Gst.TaskFunction`\. This function has to be called before calling :func:`~gi.repository.Gst.Task.pause` or :func:`~gi.repository.Gst.Task.start`. MT safe. :param mutex: The :obj:`~gi.repository.GLib.RecMutex` to use .. method:: set_pool(pool: ~gi.repository.Gst.TaskPool) -> None Set ``pool`` as the new GstTaskPool for ``task``\. Any new streaming threads that will be created by ``task`` will now use ``pool``\. MT safe. :param pool: a :obj:`~gi.repository.Gst.TaskPool` .. method:: set_state(state: ~gi.repository.Gst.TaskState) -> bool Sets the state of ``task`` to ``state``\. The ``task`` must have a lock associated with it using :func:`~gi.repository.Gst.Task.set_lock` when going to GST_TASK_STARTED or GST_TASK_PAUSED or this function will return :const:`False`. MT safe. :param state: the new task state .. method:: start() -> bool Starts ``task``\. The ``task`` must have a lock associated with it using :func:`~gi.repository.Gst.Task.set_lock` or this function will return :const:`False`. .. method:: stop() -> bool Stops ``task``\. This method merely schedules the task to stop and will not wait for the task to have completely stopped. Use :func:`~gi.repository.Gst.Task.join` to stop and wait for completion. Fields ------ .. rst-class:: interim-class .. class:: Task :no-index: .. attribute:: cond Used to pause/resume the task .. attribute:: func The function executed by this task .. attribute:: lock The lock taken when iterating the task function .. attribute:: notify GDestroyNotify for ``user_data`` .. attribute:: object .. attribute:: priv .. attribute:: running A flag indicating that the task is running .. attribute:: state The state of the task .. attribute:: thread .. attribute:: user_data User_data passed to the task function