Internal Design#

pw_console: Multi-purpose pluggable interactive console for dev & manufacturing

Threads and Event Loops#

In ptpython and IPython all user repl code is run in the foreground. This allows interrupts like Ctrl-C and functions like print() and time.sleep() to work as expected. Pigweed’s Console doesn’t use this approach as it would hide or freeze the prompt_toolkit user interface while running repl code.

To get around this issue all user repl code is run in a dedicated thread with stdout and stderr patched to capture output. This lets the user interface stay responsive and new log messages to continue to be displayed.

Here’s a diagram showing how pw_console threads and asyncio tasks are organized.

Main Thread
User Interface Event Loop
Run Code
Register Plugin
Register Plugin
Log Pane
Python Repl
User Toolbar Plugin
User Pane Plugin
Repl Thread
Repl Event Loop
Finished
Cancel with Ctrl-C
Task3
Task1
Task2
Plugin Thread 2
Plugin Event Loop 2
Refresh
UI Tokens
Pane Update Function
Plugin Thread 1
Plugin Event Loop 1
Refresh
UI Tokens
Toolbar Update Function