Skip to content

Calls to input() should flush stdout when executing cells #13039

@MaddyGuthridge

Description

@MaddyGuthridge

Environment data

  • VS Code version: 1.76.0
  • Jupyter Extension version (available under the Extensions sidebar): v2023.2.1000592019
  • Python Extension version (available under the Extensions sidebar): v2023.4.0
  • OS (Windows | Mac | Linux distro) and version: Fedora 37
  • Python and/or Anaconda version: Python 3.11.2
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Jupyter server running: Local

Expected behaviour

When I run a code block containing input and print functions, stdout is flushed whenever the input function is called, so that I can see the full output before typing my input.

Actual behaviour

The buffer only flushes once every 2 inputs on average, meaning that I often can't see the outputs of cells that I need to interact with.

Steps to reproduce:

Run a cell such as this:

while True:
    text = input("Enter some text")
    if text == '':
        print("Goodbye")
        break
    print(f"You entered {text}")

Observe that the print-out happens inconsistently.

This can be fixed by adding a hack such as the following, but it is extremely inconvenient for me to do this to all of the notebooks I need to work with.

import builtins

def input(*args, **kwargs):
    print(end='', flush=True)
    return builtins.input(*args, **kwargs)

Logs

Output for Jupyter in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Jupyter)

Visual Studio Code (1.76.0, undefined, desktop)
Jupyter Extension Version: 2023.2.1000592019.
Python Extension Version: 2023.4.0.
Workspace folder /home/migue/Source/COMP/1010/course_dev/labs-dev
User belongs to experiment group 'jupyterEnhancedDataViewer'
User belongs to experiment group 'jupyterTestcf'
info 22:26:11.323: LSP Notebooks experiment is enabled
info 22:26:11.563: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
info 22:26:28.308: Starting Jupyter Session startUsingPythonInterpreter, .jvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python.-m#ipykernel_launcher (Python Path: /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv, EnvType: Venv, EnvName: '.venv', Version: 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]) for '/home/migue/Source/COMP/1010/course_dev/labs-dev/submissions/week_02/z5350586 - Chen Du.ipynb' (disableUI=false)
info 22:26:28.364: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
info 22:26:28.409: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"68be1631-a20b-4f9f-b446-734ba592b5f2" --shell=9002 --transport="tcp" --iopub=9004 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053gMVMOvIdphcu.json
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9003 --control=9001 --hb=9000 --Session.signature_scheme="hmac-sha256" --Session.key=b"68be1631-a20b-4f9f-b446-734ba592b5f2" --shell=9002 --transport="tcp" --iopub=9004 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053gMVMOvIdphcu.json
info 22:26:28.409: Process Execution: cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
info 22:26:28.612: ipykernel version & path 6.21.1, ~/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/ipykernel/__init__.py for /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python
warn 22:26:29.048: StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.

info 22:26:29.383: Started Kernel .venv (Python 3.11.2) (pid: 321765)
info 22:26:29.431: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2023.2.1000592019/pythonFiles/printJupyterDataDir.py
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python ~/.vscode/extensions/ms-toolsai.jupyter-2023.2.1000592019/pythonFiles/printJupyterDataDir.py
info 22:27:19.756: Interrupt kernel execution
info 22:27:19.757: Interrupt requested /home/migue/Source/COMP/1010/course_dev/labs-dev/submissions/week_02/z5350586 - Chen Du.ipynb
info 22:27:19.757: Interrupt kernel execution
info 22:27:19.757: Interrupting kernel: pythonjvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd
info 22:27:19.757: Interrupting kernel via SIGINT
info 22:27:20.006: Cancel all remaining cells true || Error || undefined
info 22:27:20.008: Interrupt requested & sent for /home/migue/Source/COMP/1010/course_dev/labs-dev/submissions/week_02/z5350586 - Chen Du.ipynb in notebookEditor.
info 22:34:17.907: Starting Jupyter Session startUsingPythonInterpreter, .jvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python.-m#ipykernel_launcher (Python Path: /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv, EnvType: Venv, EnvName: '.venv', Version: 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]) for '/home/migue/Source/COMP/1010/course_dev/labs-dev/submissions/week_02/z5183911 - Allison Chen.ipynb' (disableUI=false)
info 22:34:17.944: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
info 22:34:17.980: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
info 22:34:18.029: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9008 --control=9006 --hb=9005 --Session.signature_scheme="hmac-sha256" --Session.key=b"77d8a6d6-a8eb-4495-a236-cc39864256a3" --shell=9007 --transport="tcp" --iopub=9009 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053Q05n5hr1Mu0Q.json
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9008 --control=9006 --hb=9005 --Session.signature_scheme="hmac-sha256" --Session.key=b"77d8a6d6-a8eb-4495-a236-cc39864256a3" --shell=9007 --transport="tcp" --iopub=9009 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053Q05n5hr1Mu0Q.json
info 22:34:18.029: Process Execution: cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
info 22:34:18.187: ipykernel version & path 6.21.1, ~/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/ipykernel/__init__.py for /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python
warn 22:34:18.584: StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.

info 22:34:19.010: Started Kernel .venv (Python 3.11.2) (pid: 327329)
info 22:40:29.236: Starting Jupyter Session startUsingPythonInterpreter, .jvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python.-m#ipykernel_launcher (Python Path: /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv, EnvType: Venv, EnvName: '.venv', Version: 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]) for '/home/migue/Source/COMP/1010/course_dev/labs-dev/submissions/week_02/z5367167 - Michael Chiu.ipynb' (disableUI=false)
info 22:40:29.454: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
info 22:40:29.478: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
info 22:40:29.507: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9013 --control=9011 --hb=9010 --Session.signature_scheme="hmac-sha256" --Session.key=b"d8c21f46-85ec-4040-b8b5-84974f83eb6d" --shell=9012 --transport="tcp" --iopub=9014 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053ORaRFf5PO20w.json
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9013 --control=9011 --hb=9010 --Session.signature_scheme="hmac-sha256" --Session.key=b"d8c21f46-85ec-4040-b8b5-84974f83eb6d" --shell=9012 --transport="tcp" --iopub=9014 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053ORaRFf5PO20w.json
info 22:40:29.507: Process Execution: cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
info 22:40:29.701: ipykernel version & path 6.21.1, ~/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/ipykernel/__init__.py for /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python
warn 22:40:30.088: StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.

info 22:40:30.384: Started Kernel .venv (Python 3.11.2) (pid: 331736)
info 22:42:23.003: Cancel all remaining cells true || Error || undefined
info 22:43:34.878: Starting Jupyter Session startUsingPythonInterpreter, .jvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python.-m#ipykernel_launcher (Python Path: /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv, EnvType: Venv, EnvName: '.venv', Version: 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]) for '/home/migue/Source/COMP/1010/course_dev/labs-dev/submissions/week_02/z5440815 - Clara Davila.ipynb' (disableUI=false)
info 22:43:34.905: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
info 22:43:34.950: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9018 --control=9016 --hb=9015 --Session.signature_scheme="hmac-sha256" --Session.key=b"bce614b4-7683-40c7-b8fe-d3860d6d400a" --shell=9017 --transport="tcp" --iopub=9019 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053hY07CZO6Hjkg.json
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9018 --control=9016 --hb=9015 --Session.signature_scheme="hmac-sha256" --Session.key=b"bce614b4-7683-40c7-b8fe-d3860d6d400a" --shell=9017 --transport="tcp" --iopub=9019 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053hY07CZO6Hjkg.json
info 22:43:34.950: Process Execution: cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
cwd: ~/Source/COMP/1010/course_dev/labs-dev/submissions/week_02
info 22:43:35.115: ipykernel version & path 6.21.1, ~/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/ipykernel/__init__.py for /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python
warn 22:43:35.451: StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.

warn 22:43:35.518: StdErr from Kernel Process /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/traitlets/traitlets.py:2548: FutureWarning: Supporting extra quotes around strings is deprecated in traitlets 5.0. You can use 'hmac-sha256' instead of '"hmac-sha256"' if you require traitlets >=5.
  warn(

warn 22:43:35.518: StdErr from Kernel Process /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/traitlets/traitlets.py:2499: FutureWarning: Supporting extra quotes around Bytes is deprecated in traitlets 5.0. Use 'bce614b4-7683-40c7-b8fe-d3860d6d400a' instead of 'b"bce614b4-7683-40c7-b8fe-d3860d6d400a"'.
  warn(

info 22:43:35.576: Started Kernel .venv (Python 3.11.2) (pid: 333922)
info 22:49:23.643: Starting Jupyter Session startUsingPythonInterpreter, .jvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python./home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/python.-m#ipykernel_launcher (Python Path: /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv, EnvType: Venv, EnvName: '.venv', Version: 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)]) for '/home/migue/Source/COMP/1010/course_dev/labs-dev/demo.ipynb' (disableUI=false)
info 22:49:23.681: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -c "import ipykernel; print(ipykernel.__version__); print("5dc3a68c-e34e-4080-9c3e-2a532b2ccb4d"); print(ipykernel.__file__)"
info 22:49:23.727: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m pip list
info 22:49:23.789: Process Execution: > ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9023 --control=9021 --hb=9020 --Session.signature_scheme="hmac-sha256" --Session.key=b"72c04cea-9778-48c4-961f-a9097e5c34a6" --shell=9022 --transport="tcp" --iopub=9024 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053MXxvGS4USgZ3.json
> ~/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python -m ipykernel_launcher --ip=127.0.0.1 --stdin=9023 --control=9021 --hb=9020 --Session.signature_scheme="hmac-sha256" --Session.key=b"72c04cea-9778-48c4-961f-a9097e5c34a6" --shell=9022 --transport="tcp" --iopub=9024 --f=/home/migue/.local/share/jupyter/runtime/kernel-v2-320053MXxvGS4USgZ3.json
info 22:49:23.789: Process Execution: cwd: ~/Source/COMP/1010/course_dev/labs-dev
cwd: ~/Source/COMP/1010/course_dev/labs-dev
info 22:49:23.870: ipykernel version & path 6.21.1, ~/Source/COMP/1010/course_dev/labs-dev/.venv/lib64/python3.11/site-packages/ipykernel/__init__.py for /home/migue/Source/COMP/1010/course_dev/labs-dev/.venv/bin/python
warn 22:49:24.377: StdErr from Kernel Process 0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.

info 22:49:24.613: Started Kernel .venv (Python 3.11.2) (pid: 338014)
info 22:59:12.582: Interrupt kernel execution
info 22:59:12.583: Interrupt requested /home/migue/Source/COMP/1010/course_dev/labs-dev/demo.ipynb
info 22:59:12.583: Interrupt kernel execution
info 22:59:12.583: Interrupting kernel: pythonjvsc74a57bd0ead52af248bf5110408d1f700d37b39ad1311078b7c85ad4052e3ecc15fa24dd
info 22:59:12.583: Interrupting kernel via SIGINT
info 22:59:14.578: Cancel all remaining cells true || Error || undefined
info 22:59:14.579: Interrupt requested & sent for /home/migue/Source/COMP/1010/course_dev/labs-dev/demo.ipynb in notebookEditor.

Metadata

Metadata

Labels

bugIssue identified by VS Code Team member as probable bugnotebook-executionKernels issues (start/restart/switch/execution, install ipykernel)

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions