GLib.Source

Fields

Name Type Access Description
callback_data object r  
callback_funcs GLib.SourceCallbackFuncs r  
context GLib.MainContext r  
flags int r  
name str r  
next GLib.Source r  
poll_fds [object] r  
prev GLib.Source r  
priv GLib.SourcePrivate r  
ref_count int r  
source_funcs GLib.SourceFuncs r  
source_id int r  

Methods

class new (source_funcs, struct_size)
class remove (tag)
class remove_by_funcs_user_data (funcs, user_data)
class remove_by_user_data (user_data)
class set_name_by_id (tag, name)
  add_child_source (child_source)
  add_poll (fd)
  add_unix_fd (fd, events)
  attach (context)
  destroy ()
  get_can_recurse ()
  get_context ()
  get_current_time ()
  get_id ()
  get_name ()
  get_priority ()
  get_ready_time ()
  get_time ()
  is_destroyed ()
  modify_unix_fd (tag, new_events)
  query_unix_fd (tag)
  ref ()
  remove_child_source (child_source)
  remove_poll (fd)
  remove_unix_fd (tag)
  set_callback (func, *data)
  set_callback_indirect (callback_data, callback_funcs)
  set_can_recurse (can_recurse)
  set_funcs (funcs)
  set_name (name)
  set_priority (priority)
  set_ready_time (ready_time)
  unref ()

Details

class GLib.Source(*args, **kwargs)

The GSource struct is an opaque data type representing an event source.

classmethod new(source_funcs, struct_size)[source]
Parameters:
  • source_funcs (GLib.SourceFuncs) – structure containing functions that implement the sources behavior.
  • struct_size (int) – size of the GLib.Source structure to create.
Returns:

the newly-created GLib.Source.

Return type:

GLib.Source

Creates a new GLib.Source structure. The size is specified to allow creating structures derived from GLib.Source that contain additional data. The size passed in must be at least sizeof (GSource).

The source will not initially be associated with any GLib.MainContext and must be added to one with GLib.Source.attach() before it will be executed.

classmethod remove(tag)[source]
Parameters:tag (int) – the ID of the source to remove.
Returns:For historical reasons, this function always returns True
Return type:bool

Removes the source with the given ID from the default main context. You must use GLib.Source.destroy() for sources added to a non-default main context.

The ID of a GLib.Source is given by GLib.Source.get_id(), or will be returned by the functions GLib.Source.attach(), GLib.idle_add(), GLib.idle_add(), GLib.timeout_add(), GLib.timeout_add(), GLib.child_watch_add(), GLib.child_watch_add(), GLib.io_add_watch(), and GLib.io_add_watch().

It is a programmer error to attempt to remove a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with GLib.idle_add(): the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

classmethod remove_by_funcs_user_data(funcs, user_data)[source]
Parameters:
Returns:

True if a source was found and removed.

Return type:

bool

Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.

classmethod remove_by_user_data(user_data)[source]
Parameters:user_data (object or None) – the user_data for the callback.
Returns:True if a source was found and removed.
Return type:bool

Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.

classmethod set_name_by_id(tag, name)[source]
Parameters:

Sets the name of a source using its ID.

This is a convenience utility to set source names from the return value of GLib.idle_add(), GLib.timeout_add(), etc.

It is a programmer error to attempt to set the name of a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with GLib.idle_add(): the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

New in version 2.26.

add_child_source(child_source)[source]
Parameters:child_source (GLib.Source) – a second GLib.Source that self should “poll”

Adds child_source to self as a “polled” source; when self is added to a GLib.MainContext, child_source will be automatically added with the same priority, when child_source is triggered, it will cause self to dispatch (in addition to calling its own callback), and when self is destroyed, it will destroy child_source as well. (self will also still be dispatched if its own prepare/check functions indicate that it is ready.)

If you don’t need child_source to do anything on its own when it triggers, you can call g_source_set_dummy_callback() on it to set a callback that does nothing (except return True if appropriate).

self will hold a reference on child_source while child_source is attached to it.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

New in version 2.28.

add_poll(fd)[source]
Parameters:fd (GLib.PollFD) – a GLib.PollFD structure holding information about a file descriptor to watch.

Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined with GLib.Source.new() to add an event source. The event source’s check function will typically test the revents field in the GLib.PollFD struct and return True if events need to be processed.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

Using this API forces the linear scanning of event sources on each main loop iteration. Newly-written event sources should try to use GLib.Source.add_unix_fd() instead of this API.

add_unix_fd(fd, events)[source]
Parameters:
Returns:

an opaque tag

Return type:

object

Monitors fd for the IO events in events.

The tag returned by this function can be used to remove or modify the monitoring of the fd using GLib.Source.remove_unix_fd() or GLib.Source.modify_unix_fd().

It is not necessary to remove the fd before destroying the source; it will be cleaned up automatically.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

As the name suggests, this function is not available on Windows.

New in version 2.36.

attach(context)[source]
Parameters:context (GLib.MainContext or None) – a GLib.MainContext (if None, the default context will be used)
Returns:the ID (greater than 0) for the source within the GLib.MainContext.
Return type:int

Adds a GLib.Source to a context so that it will be executed within that context. Remove it by calling GLib.Source.destroy().

destroy()[source]

Removes a source from its GLib.MainContext, if any, and mark it as destroyed. The source cannot be subsequently added to another context. It is safe to call this on sources which have already been removed from their context.

This does not unref the GLib.Source: if you still hold a reference, use GLib.Source.unref() to drop it.

get_can_recurse()[source]
Returns:whether recursion is allowed.
Return type:bool

Checks whether a source is allowed to be called recursively. see GLib.Source.set_can_recurse().

get_context()[source]
Returns:the GLib.MainContext with which the source is associated, or None if the context has not yet been added to a source.
Return type:GLib.MainContext or None

Gets the GLib.MainContext with which the source is associated.

You can call this on a source that has been destroyed, provided that the GLib.MainContext it was attached to still exists (in which case it will return that GLib.MainContext). In particular, you can always call this function on the source returned from GLib.main_current_source(). But calling this function on a source whose GLib.MainContext has been destroyed is an error.

get_current_time()[source]
Returns:Time in seconds since the Epoch
Return type:float

This function ignores self and is otherwise the same as GLib.get_current_time().

Deprecated since version 2.28: use GLib.Source.get_time() instead

get_id()[source]
Returns:the ID (greater than 0) for the source
Return type:int

Returns the numeric ID for a particular source. The ID of a source is a positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by GLib.MainContext.find_source_by_id().

You can only call this function while the source is associated to a GLib.MainContext instance; calling this function before GLib.Source.attach() or after GLib.Source.destroy() yields undefined behavior. The ID returned is unique within the GLib.MainContext instance passed to GLib.Source.attach().

get_name()[source]
Returns:the name of the source
Return type:str

Gets a name for the source, used in debugging and profiling. The name may be None if it has never been set with GLib.Source.set_name().

New in version 2.26.

get_priority()[source]
Returns:the priority of the source
Return type:int

Gets the priority of a source.

get_ready_time()[source]
Returns:the monotonic ready time, -1 for “never”
Return type:int

Gets the “ready time” of self, as set by GLib.Source.set_ready_time().

Any time before the current monotonic time (including 0) is an indication that the source will fire immediately.

get_time()[source]
Returns:the monotonic time in microseconds
Return type:int

Gets the time to be used when checking this source. The advantage of calling this function over calling GLib.get_monotonic_time() directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system monotonic time.

The time here is the system monotonic time, if available, or some other reasonable alternative otherwise. See GLib.get_monotonic_time().

New in version 2.28.

is_destroyed()[source]
Returns:True if the source has been destroyed
Return type:bool

Returns whether self has been destroyed.

This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.

static gboolean
idle_callback (gpointer data)
{
  SomeWidget *self = data;

  GDK_THREADS_ENTER ();
  // do stuff with self
  self->idle_id = 0;
  GDK_THREADS_LEAVE ();

  return G_SOURCE_REMOVE;
}

static void
some_widget_do_stuff_later (SomeWidget *self)
{
  self->idle_id = g_idle_add (idle_callback, self);
}

static void
some_widget_finalize (GObject *object)
{
  SomeWidget *self = SOME_WIDGET (object);

  if (self->idle_id)
    g_source_remove (self->idle_id);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.

static gboolean
idle_callback (gpointer data)
{
  SomeWidget *self = data;

  GDK_THREADS_ENTER ();
  if (!g_source_is_destroyed (g_main_current_source ()))
    {
      // do stuff with self
    }
  GDK_THREADS_LEAVE ();

  return FALSE;
}

Calls to this function from a thread other than the one acquired by the GLib.MainContext the GLib.Source is attached to are typically redundant, as the source could be destroyed immediately after this function returns. However, once a source is destroyed it cannot be un-destroyed, so this function can be used for opportunistic checks from any thread.

New in version 2.12.

modify_unix_fd(tag, new_events)[source]
Parameters:

Updates the event mask to watch for the fd identified by tag.

tag is the tag returned from GLib.Source.add_unix_fd().

If you want to remove a fd, don’t set its event mask to zero. Instead, call GLib.Source.remove_unix_fd().

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

As the name suggests, this function is not available on Windows.

New in version 2.36.

query_unix_fd(tag)[source]
Parameters:tag (object) – the tag from GLib.Source.add_unix_fd()
Returns:the conditions reported on the fd
Return type:GLib.IOCondition

Queries the events reported for the fd corresponding to tag on self during the last poll.

The return value of this function is only defined when the function is called from the check or dispatch functions for self.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

As the name suggests, this function is not available on Windows.

New in version 2.36.

ref()[source]
Returns:self
Return type:GLib.Source

Increases the reference count on a source by one.

remove_child_source(child_source)[source]
Parameters:child_source (GLib.Source) – a GLib.Source previously passed to GLib.Source.add_child_source().

Detaches child_source from self and destroys it.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

New in version 2.28.

remove_poll(fd)[source]
Parameters:fd (GLib.PollFD) – a GLib.PollFD structure previously passed to GLib.Source.add_poll().

Removes a file descriptor from the set of file descriptors polled for this source.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

remove_unix_fd(tag)[source]
Parameters:tag (object) – the tag from GLib.Source.add_unix_fd()

Reverses the effect of a previous call to GLib.Source.add_unix_fd().

You only need to call this if you want to remove an fd from being watched while keeping the same source around. In the normal case you will just want to destroy the source.

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

As the name suggests, this function is not available on Windows.

New in version 2.36.

set_callback(func, *data)[source]
Parameters:

Sets the callback function for a source. The callback for a source is called from the source’s dispatch function.

The exact type of func depends on the type of source; ie. you should not count on func being called with data as its first parameter. Cast func with G_SOURCE_FUNC() to avoid warnings about incompatible function types.

See memory management of sources for details on how to handle memory management of data.

Typically, you won’t use this function. Instead use functions specific to the type of source you are using, such as GLib.idle_add() or GLib.timeout_add().

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

set_callback_indirect(callback_data, callback_funcs)[source]
Parameters:
  • callback_data (object or None) – pointer to callback data “object”
  • callback_funcs (GLib.SourceCallbackFuncs) – functions for reference counting callback_data and getting the callback and data

Sets the callback function storing the data as a refcounted callback “object”. This is used internally. Note that calling GLib.Source.set_callback_indirect() assumes an initial reference count on callback_data, and thus callback_funcs->unref will eventually be called once more than callback_funcs->ref.

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

set_can_recurse(can_recurse)[source]
Parameters:can_recurse (bool) – whether recursion is allowed for this source

Sets whether a source can be called recursively. If can_recurse is True, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.

set_funcs(funcs)[source]
Parameters:funcs (GLib.SourceFuncs) – the new GLib.SourceFuncs

Sets the source functions (can be used to override default implementations) of an unattached source.

New in version 2.12.

set_name(name)[source]
Parameters:name (str) – debug name for the source

Sets a name for the source, used in debugging and profiling. The name defaults to None.

The source name should describe in a human-readable way what the source does. For example, “X11 event queue” or “GTK+ repaint idle handler” or whatever it is.

It is permitted to call this function multiple times, but is not recommended due to the potential performance impact. For example, one could change the name in the “check” function of a GLib.SourceFuncs to include details like the event type in the source name.

Use caution if changing the name while another thread may be accessing it with GLib.Source.get_name(); that function does not copy the value, and changing the value will free it while the other thread may be attempting to use it.

New in version 2.26.

set_priority(priority)[source]
Parameters:priority (int) – the new priority.

Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.

A child source always has the same priority as its parent. It is not permitted to change the priority of a source once it has been added as a child of another source.

set_ready_time(ready_time)[source]
Parameters:ready_time (int) – the monotonic time at which the source will be ready, 0 for “immediately”, -1 for “never”

Sets a GLib.Source to be dispatched when the given monotonic time is reached (or passed). If the monotonic time is in the past (as it always will be if ready_time is 0) then the source will be dispatched immediately.

If ready_time is -1 then the source is never woken up on the basis of the passage of time.

Dispatching the source does not reset the ready time. You should do so yourself, from the source dispatch function.

Note that if you have a pair of sources where the ready time of one suggests that it will be delivered first but the priority for the other suggests that it would be delivered first, and the ready time for both sources is reached during the same main context iteration, then the order of dispatch is undefined.

It is a no-op to call this function on a GLib.Source which has already been destroyed with GLib.Source.destroy().

This API is only intended to be used by implementations of GLib.Source. Do not call this API on a GLib.Source that you did not create.

New in version 2.36.

unref()[source]

Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.