GLib.MainContext¶
Fields¶
None
Methods¶
| class | default() | 
| class | get_thread_default() | 
| class | new() | 
| class | ref_thread_default() | 
| acquire() | |
| add_poll(fd, priority) | |
| check(max_priority, fds) | |
| dispatch() | |
| find_source_by_funcs_user_data(funcs, user_data) | |
| find_source_by_id(source_id) | |
| find_source_by_user_data(user_data) | |
| invoke_full(priority, function, *data) | |
| is_owner() | |
| iteration(may_block) | |
| pending() | |
| pop_thread_default() | |
| prepare() | |
| push_thread_default() | |
| query(max_priority) | |
| ref() | |
| release() | |
| remove_poll(fd) | |
| unref() | |
| wait(cond, mutex) | |
| wakeup() | 
Details¶
- 
class GLib.MainContext¶
- The - GMainContextstruct is an opaque data type representing a set of sources to be handled in a main loop.- 
classmethod default()[source]¶
- Returns: - the global default main context. - Return type: - GLib.MainContext- Returns the global default main context. This is the main context used for main loop functions when a main loop is not explicitly specified, and corresponds to the “main” main loop. See also - GLib.MainContext.get_thread_default().
 - 
classmethod get_thread_default()[source]¶
- Returns: - the thread-default - GLib.MainContext, or- Noneif the thread-default context is the global default context.- Return type: - GLib.MainContext- Gets the thread-default - GLib.MainContextfor this thread. Asynchronous operations that want to be able to be run in contexts other than the default one should call this method or- GLib.MainContext.ref_thread_default() to get a- GLib.MainContextto add their- GLib.Sourcesto. (Note that even in single-threaded programs applications may sometimes want to temporarily push a non-default context, so it is not safe to assume that this will always return- Noneif you are running in the default thread.)- If you need to hold a reference on the context, use - GLib.MainContext.ref_thread_default() instead.- New in version 2.22. 
 - 
classmethod new()[source]¶
- Returns: - the new - GLib.MainContext- Return type: - GLib.MainContext- Creates a new - GLib.MainContextstructure.
 - 
classmethod ref_thread_default()[source]¶
- Returns: - the thread-default - GLib.MainContext. Unref with- GLib.MainContext.unref() when you are done with it.- Return type: - GLib.MainContext- Gets the thread-default - GLib.MainContextfor this thread, as with- GLib.MainContext.get_thread_default(), but also adds a reference to it with- GLib.MainContext.ref(). In addition, unlike- GLib.MainContext.get_thread_default(), if the thread-default context is the global default context, this will return that- GLib.MainContext(with a ref added to it) rather than returning- None.- New in version 2.32. 
 - 
acquire()[source]¶
- Returns: - Trueif the operation succeeded, and this thread is now the owner of self.- Return type: - bool- Tries to become the owner of the specified context. If some other thread is the owner of the context, returns - Falseimmediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when- GLib.MainContext.release() is called as many times as- GLib.MainContext.acquire().- You must be the owner of a context before you can call - GLib.MainContext.prepare(),- GLib.MainContext.query(),- GLib.MainContext.check(),- GLib.MainContext.dispatch().
 - 
add_poll(fd, priority)[source]¶
- Parameters: - fd (GLib.PollFD) – aGLib.PollFDstructure holding information about a file descriptor to watch.
- priority (int) – the priority for this file descriptor which should be the same as the priority used forGLib.Source.attach() to ensure that the file descriptor is polled whenever the results may be needed.
 - Adds a file descriptor to the set of file descriptors polled for this context. This will very seldom be used directly. Instead a typical event source will use - GLib.Source.add_unix_fd() instead.
- fd (
 - 
check(max_priority, fds)[source]¶
- Parameters: - max_priority (int) – the maximum numerical priority of sources to check
- fds ([GLib.PollFD]) – array ofGLib.PollFD’s that was passed to the last call toGLib.MainContext.query()
 - Returns: - Trueif some sources are ready to be dispatched.- Return type: - Passes the results of polling back to the main loop. - You must have successfully acquired the context with - GLib.MainContext.acquire() before you may call this function.
- max_priority (
 - 
dispatch()[source]¶
- Dispatches all pending sources. - You must have successfully acquired the context with - GLib.MainContext.acquire() before you may call this function.
 - 
find_source_by_funcs_user_data(funcs, user_data)[source]¶
- Parameters: - funcs (GLib.SourceFuncs) – the source_funcs passed toGLib.Source.new().
- user_data (objectorNone) – the user data from the callback.
 - Returns: - the source, if one was found, otherwise - None- Return type: - Finds a source with the given source functions and user data. If multiple sources exist with the same source function and user data, the first one found will be returned. 
- funcs (
 - 
find_source_by_id(source_id)[source]¶
- Parameters: - source_id ( - int) – the source ID, as returned by- GLib.Source.get_id().- Returns: - the - GLib.Source- Return type: - GLib.Source- Finds a - GLib.Sourcegiven a pair of context and ID.- It is a programmer error to attempt to look up 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.
 - 
find_source_by_user_data(user_data)[source]¶
- Parameters: - user_data ( - objector- None) – the user_data for the callback.- Returns: - the source, if one was found, otherwise - None- Return type: - GLib.Source- Finds a source with the given user data for the callback. If multiple sources exist with the same user data, the first one found will be returned. 
 - 
invoke_full(priority, function, *data)[source]¶
- Parameters: - priority (int) – the priority at which to run function
- function (GLib.SourceFunc) – function to call
- data (objectorNone) – data to pass to function
 - Invokes a function in such a way that self is owned during the invocation of function. - This function is the same as g_main_context_invoke() except that it lets you specify the priority in case function ends up being scheduled as an idle and also lets you give a - GLib.DestroyNotifyfor data.- notify should not assume that it is called from any particular thread or with any particular context acquired. - New in version 2.28. 
- priority (
 - 
is_owner()[source]¶
- Returns: - Trueif current thread is owner of self.- Return type: - bool- Determines whether this thread holds the (recursive) ownership of this - GLib.MainContext. This is useful to know before waiting on another thread that may be blocking to get ownership of self.- New in version 2.10. 
 - 
iteration(may_block)[source]¶
- Parameters: - may_block ( - bool) – whether the call may block.- Returns: - Trueif events were dispatched.- Return type: - bool- Runs a single iteration for the given main loop. This involves checking to see if any event sources are ready to be processed, then if no events sources are ready and may_block is - True, waiting for a source to become ready, then dispatching the highest priority events sources that are ready. Otherwise, if may_block is- Falsesources are not waited to become ready, only those highest priority events sources will be dispatched (if any), that are ready at this given moment without further waiting.- Note that even when may_block is - True, it is still possible for- GLib.MainContext.iteration() to return- False, since the wait may be interrupted for other reasons than an event source becoming ready.
 - 
pending()[source]¶
- Returns: - Trueif events are pending.- Return type: - bool- Checks if any sources have pending events for the given context. 
 - 
pop_thread_default()[source]¶
- Pops self off the thread-default context stack (verifying that it was on the top of the stack). - New in version 2.22. 
 - 
prepare()[source]¶
- Returns: - Trueif some source is ready to be dispatched prior to polling.- priority: - location to store priority of highest priority source already ready. - Return type: - ( - bool, priority:- int)- Prepares to poll sources within a main loop. The resulting information for polling is determined by calling - GLib.MainContext.query().- You must have successfully acquired the context with - GLib.MainContext.acquire() before you may call this function.
 - 
push_thread_default()[source]¶
- Acquires self and sets it as the thread-default context for the current thread. This will cause certain asynchronous operations (such as most ‘gio [gio]’-based I/O) which are started in this thread to run under self and deliver their results to its main loop, rather than running under the global default context in the main thread. Note that calling this function changes the context returned by - GLib.MainContext.get_thread_default(), not the one returned by- GLib.MainContext.default(), so it does not affect the context used by functions like- GLib.idle_add().- Normally you would call this function shortly after creating a new thread, passing it a - GLib.MainContextwhich will be run by a- GLib.MainLoopin that thread, to set a new default context for all async operations in that thread. In this case you may not need to ever call- GLib.MainContext.pop_thread_default(), assuming you want the new- GLib.MainContextto be the default for the whole lifecycle of the thread.- If you don’t have control over how the new thread was created (e.g. in the new thread isn’t newly created, or if the thread life cycle is managed by a - GLib.ThreadPool), it is always suggested to wrap the logic that needs to use the new- GLib.MainContextinside a- GLib.MainContext.push_thread_default() /- GLib.MainContext.pop_thread_default() pair, otherwise threads that are re-used will end up never explicitly releasing the- GLib.MainContextreference they hold.- In some cases you may want to schedule a single operation in a non-default context, or temporarily use a non-default context in the main thread. In that case, you can wrap the call to the asynchronous operation inside a - GLib.MainContext.push_thread_default() /- GLib.MainContext.pop_thread_default() pair, but it is up to you to ensure that no other asynchronous operations accidentally get started while the non-default context is active.- Beware that libraries that predate this function may not correctly handle being used from a thread with a thread-default context. Eg, see g_file_supports_thread_contexts(). - New in version 2.22. 
 - 
query(max_priority)[source]¶
- Parameters: - max_priority ( - int) – maximum priority source to check- Returns: - the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored. - timeout_: - location to store timeout to be used in polling - fds: - location to store - GLib.PollFDrecords that need to be polled.- Return type: - ( - int, timeout_:- int, fds: [- GLib.PollFD])- Determines information necessary to poll this main loop. - You must have successfully acquired the context with - GLib.MainContext.acquire() before you may call this function.
 - 
ref()[source]¶
- Returns: - the self that was passed in (since 2.6) - Return type: - GLib.MainContext- Increases the reference count on a - GLib.MainContextobject by one.
 - 
release()[source]¶
- Releases ownership of a context previously acquired by this thread with - GLib.MainContext.acquire(). If the context was acquired multiple times, the ownership will be released only when- GLib.MainContext.release() is called as many times as it was acquired.
 - 
remove_poll(fd)[source]¶
- Parameters: - fd ( - GLib.PollFD) – a- GLib.PollFDdescriptor previously added with- GLib.MainContext.add_poll()- Removes file descriptor from the set of file descriptors to be polled for a particular context. 
 - 
unref()[source]¶
- Decreases the reference count on a - GLib.MainContextobject by one. If the result is zero, free the context and free all associated memory.
 - 
wait(cond, mutex)[source]¶
- Parameters: - cond (GLib.Cond) – a condition variable
- mutex (GLib.Mutex) – a mutex, currently held
 - Returns: - Trueif the operation succeeded, and this thread is now the owner of self.- Return type: - Tries to become the owner of the specified context, as with - GLib.MainContext.acquire(). But if another thread is the owner, atomically drop mutex and wait on cond until that owner releases ownership or until cond is signaled, then try again (once) to become the owner.- Deprecated since version 2.58: Use - GLib.MainContext.is_owner() and separate locking instead.
- cond (
 - 
wakeup()[source]¶
- If self is currently blocking in - GLib.MainContext.iteration() waiting for a source to become ready, cause it to stop blocking and return. Otherwise, cause the next invocation of- GLib.MainContext.iteration() to return without blocking.- This API is useful for low-level control over - GLib.MainContext; for example, integrating it with main loop implementations such as- GLib.MainLoop.- Another related use for this function is when implementing a main loop with a termination condition, computed from multiple threads: - #define NUM_TASKS 10 static volatile gint tasks_remaining = NUM_TASKS; ... while (g_atomic_int_get (&tasks_remaining) != 0) g_main_context_iteration (NULL, TRUE); - Then in a thread: - perform_work(); if (g_atomic_int_dec_and_test (&tasks_remaining)) g_main_context_wakeup (NULL); 
 
- 
classmethod