Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python input blocked when called from batch within an "if" and after a "timeout"

Minimum working example:

test.bat:

@echo off
if 0==0 (
  timeout 3
  python test.py
)

test.py:

input('press ENTER to exit')

In cmd.exe:

call test.bat
> Waiting for 0 seconds, press a key to continue ...
> press ENTER to exit_

Then it's stuck at the input statement. If I keep pressing enter, it will eventually raise an exception:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    input('press ENTER to exit')
OSError: [WinError 8] Not enough storage is available to process this command

I'm on Windows 7 SP1, tried it with

  • Python 3.6 (64-bit)
  • Python 3.6 (32-bit)
  • Python 3.5 (32-bit)
  • Python 2.7 (32-bit) (in this case it's stuck forever without raising exception)

Can anyone reproduce this and/or have any idea where it went wrong?

like image 970
Chenfeng Avatar asked Mar 28 '26 02:03

Chenfeng


1 Answers

I can reproduce the issue on Windows 7 with Python 3.6.5 64 bit.

A couple of alternatives to avoid the issue with timeout:

@echo off
if 0==0 (
  echo Pause for 3 seconds
  ping -n 3 localhost >nul
  python test.py
)
exit /b

This uses ping which causes no issue.

@echo off
if 0==0 (
  timeout 3
  call :run_python test.py
)
exit /b

:run_python
python %*
exit /b

This uses a label named :run_python which allows the interpreter to run Python outside the context of the parentheses. Any arguments passed following the called label will be passed to python.

like image 112
michael_heath Avatar answered Mar 31 '26 03:03

michael_heath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!