Trampoline to Main Thread

Greenlet-based main-thread trampolining for UIs.

During the normal execution of a function, it will be running in the main thread. However, special functions (provided by this module) will allow for that function to be interrupted, leave the main thread, wait for some action, and then resume the function back in the main thread.

@trampoline.decorate(threads.call_in_main_thread)
def test_something(self):

    # We are running in the main thread.
    dialog = my_tool.run()

    # Fall back out of the main event loop while we want for the following
    # to become true.
    button = trampoline.qpath(dialog, '//QPushButton[@enabled]', timeout=5)[0]

    # Do something else in the main thread.

    # Wait a second while letting the event loop resume.
    trampoline.sleep(1)
uitools.trampoline.trampoline(call_in_main_thread, func, *args, **kwargs)[source]

Call a function in the main thread, allowing it to bounce into the background.

When called via trampoline a function’s main body will execute in the main thread. However, it is allowed to use bounce() (and functions which use bounce()) to effectively run synchronous tasks in the background while giving the main thread a chance to perform other tasks.

def my_func():
    print 'Going to sleep (without locking up the main thread)...'
    bounce(sleep, 10)
    print 'Back!'
Parameters:
  • call_in_main_thread – Function to call another function in the main thread.
  • func – The function to run.
  • *args

    Passed to func.

  • **kwargs

    Passed to func.

uitools.trampoline.decorate(call_in_main_thread)[source]

Decorate a function so that its body runs in the main thread as if called via trampoline().

uitools.trampoline.bounce(func=None, *args, **kwargs)[source]

Perform an effectively sychnorous call in the background from a trampolined function.

Parameters:
  • func – The function to call.
  • *args

    Passed to func.

  • **kwargs

    Passed to func.

Returns:

The return value of func.

Raises :

Anything that func raises.

uitools.trampoline.sleep(seconds)[source]

Sleep in the background.

uitools.trampoline.qpath(root, query, timeout=5, repeat_delay=0.033, strict=False)[source]

Project Versions

Previous topic

Threads

Next topic

Querying Qt Graphs

This Page