-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stdio in mo.Thread goes to Linux console after sleep() #3532
Comments
From discord:
|
This seems to do the trick. The only changes are to If this doesn't look too crazy to you, I'll put together a test and a pull request: class Thread(threading.Thread):
"""A Thread subclass that is aware of marimo internals.
`mo.Thread` has the same API as threading.Thread,
but `mo.Thread`s are able to communicate with the marimo
frontend, whereas `threading.Thread` can't.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._marimo_ctx: RuntimeContext | None = None
if runtime_context_installed():
# self._marimo_ctx = get_context()
parent_ctx = get_context()
parent_stream = parent_ctx.stream
new_stream = ThreadSafeStream(
pipe=parent_stream.pipe,
input_queue=parent_stream.input_queue,
cell_id=parent_stream.cell_id,
)
new_stdout = ThreadSafeStdout(new_stream)
new_stderr = ThreadSafeStderr(new_stream)
sys.stdout = new_stdout
sys.stderr = new_stderr
self._marimo_ctx = KernelRuntimeContext(**parent_ctx.__dict__)
self._marimo_ctx.stdout = new_stdout
self._marimo_ctx.stderr = new_stderr
new_stdout = new_stdout._watcher.start()
new_stderr = new_stderr._watcher.start()
def run(self) -> None:
if self._marimo_ctx is not None:
initialize_context(self._marimo_ctx)
super().run() |
I think the approach is good -- few small things can bring up in a PR review. Thanks so much! EDIT: Took a closer look, I think it may be more complicated, see follow-up comment. |
It's been a while since I've looked at this code, so I took a closer look and I think it may be more complicated. This approach might not work when there are multiple Something like the following could work (but without diving deeper I am not 100% sure): share the same Even still there may be some limitations, such as Let me know if you're still comfortable with attempting this change. No worries if not. |
Describe the bug
When running the following code, the first two print statements are shown in the marimo cell, and all the
print
statements that follow the firstsleep
end up bring printed in the stdout of the Linux shell where the marimo command was run.If the
sleep
statement is commented out, then allprint
statements end up in the marimo cell.Issue #2642 seems to imply that all I/O should go to the notebook if using
mo.Thread
Environment
Code to reproduce
The text was updated successfully, but these errors were encountered: