Skip to content
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

Input() never returns when InteractiveShellEmbed is used? #12078

Open
gigaj0ule opened this issue Jan 8, 2020 · 2 comments
Open

Input() never returns when InteractiveShellEmbed is used? #12078

gigaj0ule opened this issue Jan 8, 2020 · 2 comments

Comments

@gigaj0ule
Copy link

gigaj0ule commented Jan 8, 2020

I'm pretty stuck on this one here...

When I use an instance of InteractiveShellEmbed, if Input() is called from another thread, the program will freeze. Not even CTRL-C can break it!

It seems to be the case that Stdin is somehow bound by the interactiveshell, and so input() never receives any data. Further, attempts to directly get data from Stdin with a readline call fail also. Sometimes, I am able to get characters, but never a newline character.

Is there a way to either,

  • Call input() in the interactiveshell programatically? (sadly, run_cell on the instance, will also hang...)
  • Detach Stdin temporarily from interactiveshell?
  • Bind a service that captures a copy of Stdin and forwards it to something else?
  • Use some secret input method I am not aware of :-)

I am willing to put in the work to fix this if someone can point me in the right direction!

@Carreau
Copy link
Member

Carreau commented Jan 18, 2020

Thanks for the report; that might be due to prompt toolkit hijacking input; there is likely a context manager to get a stdin that works. I know there is with patch_stdout (for stdout) but I would have to figure out for stdin.

@reszelaz
Copy link

Hi,
I'm not sure but I think I hit the same problem, I could reproduce it using this simple script: ipython_input.py.txt, in the following way:

╰─>$ ipython                                                                                                                                                                                                                                                         (ipython) 
Python 3.9.4 | packaged by conda-forge | (default, May 10 2021, 22:13:33) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.24.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: load workspace/ipython_input.py

In [2]: # %load workspace/ipython_input.py
   ...: import threading
   ...: import time
   ...: 
   ...: 
   ...: def foo():
   ...:     print("foo() entering")
   ...:     input("Type sth:")
   ...:     print("foo() exiting")
   ...: 
   ...: 
   ...: def bar():
   ...:     print("bar() entering")
   ...:     th = threading.Thread(target=foo)
   ...:     th.start()
   ...:     time.sleep(3)
   ...:     print("thread is alive: %s" % th.is_alive())
   ...:     print("bar() exiting")
   ...:     return th
   ...: 
   ...: th = bar()
   ...: print("thread is alive: %s" % th.is_alive())
   ...: 
bar() entering
foo() entering
Type sth:thread is alive: True
bar() exiting
thread is alive: True

In [3]:

After executing it, IPython hangs.
When I run the same script in a Python console, it works well.

reszelaz added a commit to reszelaz/sardana that referenced this issue Jun 16, 2021
When macro input gives timeout the Spock (IPython) gets hung completely.
It seems to be bug in IPython - see ipython/ipython#12078.
Use prompt_toolkit.prompt() (a dependency of IPython) instead of input() and raise an
exception in the thread waiting for input when the timeout was reached. This apparently
works.

Also change the message in Spock when the timeout is reached.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants