python from threading import Thread import time def long_running(): print("Thread starting:") i = 0 while True: print(f"Loop {i}...") i += 1 time.sleep(1) print("Thread exiting.") t = Thread(target=long_running) t.start()
KernelRuntimeContext
associated with it and it still has the same Stdout
objectconsole_output_worker
that has some timing loop associated with it, but it's not clear to me why it would print to console instead of just dropping the outputs: https://github.com/marimo-team/marimo/blob/01c1745634ede31b419e70ef484d34e128192033/marimo/_messaging/console_output_worker.pymo.Thread
is initialized with the parent thread's context, but I believe it needs its own context. What's happening now is that after the cell that creates the mo.Thread
finishes executing, the parent thread's Stdout
is correctly disconnected from the cell, but this disconnects the mo.Thread
's Stdout
too.Stdout
and Stderr
objects redirected, similar to this: https://github.com/marimo-team/marimo/blob/b9402ce516dd102464fe5a3cb75a979dc8179cc0/marimo/_runtime/runtime.py#L648-L654. You can use the cell_id
of the parent thread's context. But you likely don't want to use redirect_streams
as a context manager, since they should be permanently redirected.