By calling

import rapidgui
app = rapidgui.load("example.json")

two things happen:

  1. For each component defined in example.json, a GuiCallProxy instance is created and such that it is returned by calling app(identifier). GuiCallProxy creates a queue and puts all calls made to any arbitrary attribute to it onto it. Through the __call__ method is also returns a decorator that, if called, puts the call add_event_listener with the passed function onto the queue.
  2. It spins up a new thread that imports gui_components and creates a new GuiApp instance. The import is only done in the new thread because pyglet, the graphical backend, only supports windows created on the thread it is imported on. The new thread then creates an actual GuiComponent-derivative instance for each component defined in example.json, passing the queue created by the corresponding GuiCallProxy. Finally, it starts the app.

Each GuiComponent pops all elements off its queue about 60 timer per second within the gui thread by using the pyglet event loop. After popping off each element, it executes the call encoded within that element on itself, giving the client the impression that the call on GuiCallProxy was actually done on the corresponding GuiComponent.

If a method returns a value, that value has to be returned from the gui thread back to the thread calling the method. This means that the the main thread and the gui thread have to be synchronized: The main thread has to wait before returning until the gui thread has executed the call and returned a value. Because most methods likely won't return a value at all, and thus performance cycles are wasted on waiting on the other thread, it is implicitly assumed that methods return a value if and only if their name starts with get_.... If a method with such a name is called, a new queue instance is put onto the queue along with the encoded call itself; the caller thread then waits until the return value has been put onto this queue before returning.


GuiComponent is implemented in such a way that whenever the gui thread dispatches an event that has callbacks from the client (e.g. on_pressed for a button), that callback is started in a new thread. The reason for this is that a) to avoid deadlocks that happen if the callback starts a getter method (the gui thread waiting for the gui thread to put a value onto the return-queue) and b) because no assumptions should be made that a client method returns soon or at all. A callback may take seconds before finishing, but the gui thread should still proceed with drawing 60fps.

Currently, each callback is limited to at most one thread running it at once. This is to avoid long tasks running many times at once when a user clicks a button many times in a row. This behavior might be improved in the future, e.g. by giving the user a boolean option run_multiple_callback_instances that either disables the button until the thread is done or launches an arbitrary amount of threads.

To recap, there are three ways to interact with the gui thread:

  1. Put @app(identifier) as a decorator above a function definition - puts call ("add_event_listener", [f], {}, queue_dummy) . Which event is listened to is determined by the name of the function.
  2. Call a non-returning function: app(identifier).set_size(2, q=3) , this puts ("set_size", [2], {"q": 3}, queue_dummy) onto the queue.
  3. Call a returning function: app(identifier).get_size(), this puts ("get_size", [], {}, new_queue) onto the queue. The caller waits until the gui thread has placed the return value of the queue onto new_queue and then returns this value.