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.37k stars 716 forks source link

Writing to terminal feels unresponsive #916

Open ghost opened 5 years ago

ghost commented 5 years ago
def default():
    vt_key = None  
    if confirm('Use vt?'):
           vt_key = prompt('Key:', validator=VTKeyValidator(), validate_while_typing=False)

In this case, VTKeyValidator is using the network to validate the input. My problem is that simply typing in terminal takes half a second to display the typed character.

jonathanslenders commented 5 years ago

Hi @namedLambda,

Did you try wrapping this VTKeyValidator into a ThreadedValidator? See: https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/validation.py#L122

           vt_key = prompt('Key:', validator=ThreadedValidator(VTKeyValidator()), validate_while_typing=False)

That will run the validation code in a different thread. Let me know if that doesn't help.

ghost commented 5 years ago

This works great. So if I got this correct, validate_while_typing=False simply suppresses validation errors until a new line or EOF but the process still runs. Is that correct?

jonathanslenders commented 5 years ago

I think this could be a bug, I have to check it. The validator is not supposed to run on every key press if validate_while_typing is False.

ghost commented 5 years ago

That would explain the behavior as the validator I am using is connecting to a server and accounting for that justifies the observed delay. Would you like any more information/code for this?