MainLoop and Event Loops

MainLoop

class urwid.MainLoop(widget, palette=(), screen=None, handle_mouse=True, input_filter=None, unhandled_input=None, event_loop=None, pop_ups=False)

This is the standard main loop implementation for a single interactive session.

Parameters:
  • widget (widget instance) – the topmost widget used for painting the screen, stored as widget and may be modified. Must be a box widget.
  • palette (iterable of palette entries) – initial palette for screen
  • screen (display module screen instance) – screen to use, default is a new raw_display.Screen instance; stored as screen
  • handle_mouse (bool) – True to ask screen to process mouse events
  • input_filter (callable) – a function to filter input before sending it to widget, called from input_filter()
  • unhandled_input (callable) – a function called when input is not handled by widget, called from unhandled_input()
  • event_loop (event loop instance) – if screen supports external an event loop it may be given here, default is a new SelectEventLoop instance; stored as event_loop
  • pop_ups (boolean) – True to wrap widget with a PopUpTarget instance to allow any widget to open a pop-up anywhere on the screen
screen

The screen object this main loop uses for screen updates and reading input

event_loop

The event loop object this main loop uses for waiting on alarms and IO

draw_screen()

Render the widgets and paint the screen. This method is called automatically from entering_idle().

If you modify the widgets displayed outside of handling input or responding to an alarm you will need to call this method yourself to repaint the screen.

entering_idle()

This method is called whenever the event loop is about to enter the idle state. draw_screen() is called here to update the screen when anything has changed.

input_filter(keys, raw)

This function is passed each all the input events and raw keystroke values. These values are passed to the input_filter function passed to the constructor. That function must return a list of keys to be passed to the widgets to handle. If no input_filter was defined this implementation will return all the input events.

process_input(keys)

This method will pass keyboard input and mouse events to widget. This method is called automatically from the run() method when there is input, but may also be called to simulate input from the user.

keys is a list of input returned from screen‘s get_input() or get_input_nonblocking() methods.

Returns True if any key was handled by a widget or the unhandled_input() method.

remove_alarm(handle)

Remove an alarm. Return True if handle was found, False otherwise.

remove_watch_file(handle)

Remove a watch file. Returns True if the watch file exists, False otherwise.

remove_watch_pipe(write_fd)

Close the read end of the pipe and remove the watch created by watch_pipe(). You are responsible for closing the write end of the pipe.

Returns True if the watch pipe exists, False otherwise

run()

Start the main loop handling input events and updating the screen. The loop will continue until an ExitMainLoop exception is raised.

If you would prefer to manage the event loop yourself, don’t use this method. Instead, call start() before starting the event loop, and stop() once it’s finished.

set_alarm_at(tm, callback, user_data=None)

Schedule an alarm at tm time that will call callback from the within the run() function. Returns a handle that may be passed to remove_alarm().

Parameters:
  • tm (float) – time to call callback e.g. time.time() + 5
  • callback (callable) – function to call with two parameters: this main loop object and user_data
set_alarm_in(sec, callback, user_data=None)

Schedule an alarm in sec seconds that will call callback from the within the run() method.

Parameters:
  • sec (float) – seconds until alarm
  • callback (callable) – function to call with two parameters: this main loop object and user_data
start()

Sets up the main loop, hooking into the event loop where necessary. Starts the screen if it hasn’t already been started.

If you want to control starting and stopping the event loop yourself, you should call this method before starting, and call stop once the loop has finished. You may also use this method as a context manager, which will stop the loop automatically at the end of the block:

with main_loop.start():
...

Note that some event loop implementations don’t handle exceptions specially if you manage the event loop yourself. In particular, the Twisted and asyncio loops won’t stop automatically when ExitMainLoop (or anything else) is raised.

stop()

Cleans up any hooks added to the event loop. Only call this if you’re managing the event loop yourself, after the loop stops.

unhandled_input(input)

This function is called with any input that was not handled by the widgets, and calls the unhandled_input function passed to the constructor. If no unhandled_input was defined then the input will be ignored.

input is the keyboard or mouse input.

The unhandled_input function should return True if it handled the input.

watch_file(fd, callback)

Call callback when fd has some data to read. No parameters are passed to callback.

Returns a handle that may be passed to remove_watch_file().

watch_pipe(callback)

Create a pipe for use by a subprocess or thread to trigger a callback in the process/thread running the main loop.

Parameters:callback (callable) – function taking one parameter to call from within the process/thread running the main loop

This method returns a file descriptor attached to the write end of a pipe. The read end of the pipe is added to the list of files event_loop is watching. When data is written to the pipe the callback function will be called and passed a single value containing data read from the pipe.

This method may be used any time you want to update widgets from another thread or subprocess.

Data may be written to the returned file descriptor with os.write(fd, data). Ensure that data is less than 512 bytes (or 4K on Linux) so that the callback will be triggered just once with the complete value of data passed in.

If the callback returns False then the watch will be removed from event_loop and the read end of the pipe will be closed. You are responsible for closing the write end of the pipe with os.close(fd).

widget

Property for the topmost widget used to draw the screen. This must be a box widget.

SelectEventLoop

class urwid.SelectEventLoop

Event loop based on select.select()

alarm(seconds, callback)

Call callback() a given time from now. No parameters are passed to callback.

Returns a handle that may be passed to remove_alarm()

seconds – floating point time to wait before calling callback callback – function to call from event loop

enter_idle(callback)

Add a callback for entering idle.

Returns a handle that may be passed to remove_idle()

remove_alarm(handle)

Remove an alarm.

Returns True if the alarm exists, False otherwise

remove_enter_idle(handle)

Remove an idle callback.

Returns True if the handle was removed.

remove_watch_file(handle)

Remove an input file.

Returns True if the input file exists, False otherwise

run()

Start the event loop. Exit the loop when any callback raises an exception. If ExitMainLoop is raised, exit cleanly.

watch_file(fd, callback)

Call callback() when fd has some data to read. No parameters are passed to callback.

Returns a handle that may be passed to remove_watch_file()

fd – file descriptor to watch for input callback – function to call when input is available

GLibEventLoop

class urwid.GLibEventLoop

Event loop based on GLib.MainLoop

alarm(seconds, callback)

Call callback() a given time from now. No parameters are passed to callback.

Returns a handle that may be passed to remove_alarm()

seconds – floating point time to wait before calling callback callback – function to call from event loop

enter_idle(callback)

Add a callback for entering idle.

Returns a handle that may be passed to remove_enter_idle()

handle_exit(f)

Decorator that cleanly exits the GLibEventLoop if ExitMainLoop is thrown inside of the wrapped function. Store the exception info if some other exception occurs, it will be reraised after the loop quits.

f – function to be wrapped

remove_alarm(handle)

Remove an alarm.

Returns True if the alarm exists, False otherwise

remove_enter_idle(handle)

Remove an idle callback.

Returns True if the handle was removed.

remove_watch_file(handle)

Remove an input file.

Returns True if the input file exists, False otherwise

run()

Start the event loop. Exit the loop when any callback raises an exception. If ExitMainLoop is raised, exit cleanly.

watch_file(fd, callback)

Call callback() when fd has some data to read. No parameters are passed to callback.

Returns a handle that may be passed to remove_watch_file()

fd – file descriptor to watch for input callback – function to call when input is available

TwistedEventLoop

class urwid.TwistedEventLoop(reactor=None, manage_reactor=True)

Event loop based on Twisted

Parameters:reactor (twisted.internet.reactor.) – reactor to use
Param:manage_reactor: True if you want this event loop to run and stop the reactor.

Warning

Twisted’s reactor doesn’t like to be stopped and run again. If you need to stop and run your MainLoop, consider setting manage_reactor=False and take care of running/stopping the reactor at the beginning/ending of your program yourself.

You can also forego using MainLoop‘s run() entirely, and instead call start() and stop() before and after starting the reactor.

alarm(seconds, callback)

Call callback() a given time from now. No parameters are passed to callback.

Returns a handle that may be passed to remove_alarm()

seconds – floating point time to wait before calling callback callback – function to call from event loop

enter_idle(callback)

Add a callback for entering idle.

Returns a handle that may be passed to remove_enter_idle()

handle_exit(f, enable_idle=True)

Decorator that cleanly exits the TwistedEventLoop if ExitMainLoop is thrown inside of the wrapped function. Store the exception info if some other exception occurs, it will be reraised after the loop quits.

f – function to be wrapped

remove_alarm(handle)

Remove an alarm.

Returns True if the alarm exists, False otherwise

remove_enter_idle(handle)

Remove an idle callback.

Returns True if the handle was removed.

remove_watch_file(handle)

Remove an input file.

Returns True if the input file exists, False otherwise

run()

Start the event loop. Exit the loop when any callback raises an exception. If ExitMainLoop is raised, exit cleanly.

watch_file(fd, callback)

Call callback() when fd has some data to read. No parameters are passed to callback.

Returns a handle that may be passed to remove_watch_file()

fd – file descriptor to watch for input callback – function to call when input is available

TornadoEventLoop

class urwid.TornadoEventLoop(ioloop=None)

This is an Urwid-specific event loop to plug into its MainLoop. It acts as an adaptor for Tornado’s IOLoop which does all heavy lifting except idle-callbacks.

Notice, since Tornado has no concept of idle callbacks we monkey patch ioloop._impl.poll() function to be able to detect potential idle periods.

class PollProxy(poll_obj, idle_map)

A simple proxy for a Python’s poll object that wraps the .poll() method in order to detect idle periods and call Urwid callbacks

AsyncioEventLoop

class urwid.AsyncioEventLoop(**kwargs)

Event loop based on the standard library asyncio module.

asyncio is new in Python 3.4, but also exists as a backport on PyPI for Python 3.3. The trollius package is available for older Pythons with slightly different syntax, but also works with this loop.

alarm(seconds, callback)

Call callback() a given time from now. No parameters are passed to callback.

Returns a handle that may be passed to remove_alarm()

seconds – time in seconds to wait before calling callback callback – function to call from event loop

enter_idle(callback)

Add a callback for entering idle.

Returns a handle that may be passed to remove_idle()

remove_alarm(handle)

Remove an alarm.

Returns True if the alarm exists, False otherwise

remove_enter_idle(handle)

Remove an idle callback.

Returns True if the handle was removed.

remove_watch_file(handle)

Remove an input file.

Returns True if the input file exists, False otherwise

run()

Start the event loop. Exit the loop when any callback raises an exception. If ExitMainLoop is raised, exit cleanly.

watch_file(fd, callback)

Call callback() when fd has some data to read. No parameters are passed to callback.

Returns a handle that may be passed to remove_watch_file()

fd – file descriptor to watch for input callback – function to call when input is available