Mainloop plugins

Most GUI applications need a main loop. A main loop is a thread dedicated to running callbacks provided by other threads (or stacked before the main loop is started).

Depending on the environment in which you’re working, you may need different mainloop implementations. For instance, Python 3 provide main loop support through asyncio. If you’re working in a GTK environment, you will have to use the GLib mainloop instead. Openpaperwork_core provides an implementation that uses Python 3’s asyncio module.

More advanced main loops may provide features such as waiting for an event to occur on a file descriptor. The main loop interface provided here is kept simple so it can easily be implemented for any other platforms.

class openpaperwork_core.mainloop_asyncio.Plugin[source]

A main loop based on asyncio. Not as complete as GLib main loop, but good enough for shell commands.


Wait for callbacks to be scheduled and execute them.

This method is blocking and will block until mainloop_quit*() is called.

mainloop_execute(func, *args, **kwargs)[source]

Ensure a function is run on the main loop, even if called from a thread. Will return only once the callback func has been executed. Will return the value returned by func.

This method makes it easier to work with non-thread-safe modules (sqlite3 for instance).


Gets the ID of the thread running the main loop. None if no thread is running it.


Wait for all the scheduled callbacks to be executed and then stops the main loop.


Stops the main loop right now.

Note that it cannot interrupt a callback being executed, but no callback scheduled after this one will be executed.


If you run a task independently from the main loop, you may want to increment the reference counter of the main loop so mainloop_quit_graceful does not interrupt the main loop while your task is still running.

ThreadedPromise already takes care of incrementing and decrementing this reference counter.

schedule(func, *args, delay_s=0, **kwargs)[source]

Request that the main loop executes the callback func. Will return immediately.