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

Find a more robust way to request input for y/n decision #49

Open
akashdhruv opened this issue Oct 3, 2023 · 6 comments
Open

Find a more robust way to request input for y/n decision #49

akashdhruv opened this issue Oct 3, 2023 · 6 comments

Comments

@akashdhruv
Copy link
Member

Refer to lib/info.py where user input is requested to overwrite test.info. This issue is a result of Lab-Notebooks/Jobrunner#3 (comment)

@kweide
Copy link
Member

kweide commented Oct 3, 2023

Maybe one of the following helps:

  • Launch flashxtest, maybe also subcommands, with stdbuf --output=L --error=0 .... Maybe you are already doing that. Maybe --output should also use 0 instead of L.
  • Maybe use the -u command line flag of the python command or the PYTHONUNBUFFERED environment variable (maybe in combination with the previous item).

@akashdhruv
Copy link
Member Author

Maybe one of the following helps:

  • Launch flashxtest, maybe also subcommands, with stdbuf --output=L --error=0 .... Maybe you are already doing that. Maybe --output should also use 0 instead of L.
  • Maybe use the -u command line flag of the python command or the PYTHONUNBUFFERED environment variable (maybe in combination with the previous item).

I will try it.

@fickas
Copy link
Collaborator

fickas commented Nov 14, 2023

Suggestion by chatGPT:

import subprocess

with open("job.output", "w") as output:
    with subprocess.Popen(f"python info.py".split(),
                          stdin=subprocess.PIPE,
                          stdout=output,
                          stderr=subprocess.STDOUT,
                          text=True) as process:
        # Wait for user input
        user_input = input(f"Enter input for info.py: ")
        process.communicate(input=user_input)

@akashdhruv
Copy link
Member Author

akashdhruv commented Nov 14, 2023

Suggestion by chatGPT:

import subprocess

with open("job.output", "w") as output:
    with subprocess.Popen(f"python info.py".split(),
                          stdin=subprocess.PIPE,
                          stdout=output,
                          stderr=subprocess.STDOUT,
                          text=True) as process:
        # Wait for user input
        user_input = input(f"Enter input for info.py: ")
        process.communicate(input=user_input)

subprocess does not ask for input every time, so waiting for input like this may not be the best solution.

@fickas
Copy link
Collaborator

fickas commented Nov 14, 2023

Confused. Put it in a conditional depending on whether it needs input or not. All this does is wrap your input() code. Where you do choose to do input() wrap like this.

I take it back. I see your point about not always needing input. But not giving up yet :)

@fickas
Copy link
Collaborator

fickas commented Nov 14, 2023

How about this (runs in info.py):

if need_input:
    # Temporarily redirect stdout and stderr to the console for user interaction
    original_stdout = sys.stdout
    original_stderr = sys.stderr

    sys.stdout = sys.stderr = open('/dev/tty', 'w')

    try:
        user_input = input("Enter input: ")
        # Continue processing user_input as needed
    finally:
        # Restore the original stdout and stderr
        sys.stdout = original_stdout
        sys.stderr = original_stderr

# Continue with the rest of your script

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