prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.25k stars 714 forks source link

Exception when printing from thread in interactive mode #1040

Open jaguarondi opened 4 years ago

jaguarondi commented 4 years ago

I get this exception when I have asynchronous logging on the terminal while in interactive mode as soon as I hit enter with a command. The following code can reproduce the problem:

#!/usr/bin/env python3
import threading
import time

msg = b'This is the message.  It will be repeated.'

def UDP_receive():
    while True:
        print(msg)
        time.sleep(0.01)

listen_UDP = threading.Thread(target=UDP_receive)
listen_UDP.start()

which gives

...
b'This is the message.  It will be repeated.'
b'This is the message.  It will be repeated.'
In [1]: sdcs # just type something and hit enter
Exception in default exception handler
Traceback (most recent call last):
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/eventloop/posix.py", line 154, in _run_task
    t()
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/eventloop/context.py", line 115, in new_func
    return func(*a, **kw)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/patch_stdout.py", line 102, in write_and_flush_in_loop
    run_in_terminal(write_and_flush, in_executor=False)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 41, in run_in_terminal
    return run_coroutine_in_terminal(async_func, render_cli_done=render_cli_done)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 62, in run_coroutine_in_terminal
    assert app._is_running
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/eventloop/base.py", line 129, in call_exception_handler
    self._exception_handler(context)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 702, in handle_exception
    run_coroutine_in_terminal(print_exception)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 62, in run_coroutine_in_terminal
    assert app._is_running
AssertionError
b'This is the message.  It will be repeated.'
b'This is the message.  It will be repeated.'
...

test.py.txt

jonathanslenders commented 4 years ago

@jaguarondi,

The example script doesn't contain any prompt_toolkit related code. Can you provide a full snippet that causes this error?