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

Allow printing while waiting for user input in readline mode #41

Merged
merged 2 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions aioconsole/rlwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import signal
import builtins
import subprocess
from concurrent.futures import ThreadPoolExecutor

from . import compat

Expand All @@ -30,22 +31,43 @@ def _rlwrap(process, prompt_control, use_stderr=False):
# Check prompt control
assert len(prompt_control) == 1

# Loop over prompts
while process.poll() is None:
try:
prompt = wait_for_prompt(
source, dest, prompt_control)
raw = input(prompt, use_stderr=use_stderr) + '\n'
except KeyboardInterrupt:
process.send_signal(signal.SIGINT)
except EOFError:
process.stdin.close()
else:
process.stdin.write(raw)

# Clean up
dest.write(source.read())
return process.returncode
# Run background task
with ThreadPoolExecutor(1) as executor:
future = executor.submit(
wait_for_prompt, source, dest, prompt_control)

# Loop over prompts
while process.poll() is None:

# Get prompt
try:
prompt = future.result()
except KeyboardInterrupt:
process.send_signal(signal.SIGINT)
continue
except EOFError:
break
else:
future = executor.submit(
wait_for_prompt, source, dest, prompt_control)

# Get user input
try:
raw = input(prompt, use_stderr=use_stderr) + '\n'
except KeyboardInterrupt:
process.send_signal(signal.SIGINT)
continue
except EOFError:
break
else:
process.stdin.write(raw)

# Close and wait process streams
process.stdin.close()
future.exception()

# Wait process and return code
return process.wait()


def wait_for_prompt(src, dest, prompt_control, buffersize=1):
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
entry_points={'console_scripts': ['apython = aioconsole:run_apython']},

setup_requires=['pytest-runner' if TESTING else ''],
tests_require=['pytest',
'pytest-asyncio<0.6;python_version=="3.4"',
tests_require=['pytest;python_version>"3.4"',
'pytest-asyncio>=0.8;python_version>"3.4"',
'pytest<4;python_version=="3.4"',
'pytest-asyncio<0.6;python_version=="3.4"',
'pytest-cov'],

license="GPLv3",
Expand Down