I have Python 3.11.9 on Windows 10. I don’t use an IDE. I’m running my program in a cmd.exe window.
When debugging a program I use loops in my program, and also time.sleep(1) to pause after I print a message in a cmd.exe console. Something is causing problems where I cannot hit ^S to pause the debug messages printing on my console. I suspect the problem is with time.sleep().
So how can I pause my program output to look at it while using time.sleep() as well?
Gemini AI says not to use time.sleep() for the simplest fix. It also says to use x = input() to wait for input but I don’t want to pause after every iteration of the loop and enter a key. I only sometimes want to stop the program output in cmd.exe and see the error messages.
Try pipeing the program output through the more command.
I assume Ctrl-S does not work for you because you are on Windows 10 or not using the Terminal app (recommended on windows).
I’m just using cmd.exe. I use a desktop shortcut, here is my shortcut program: C:\Windows\System32\cmd.exe /k c:\MYUSER\addpath.bat addpath.bat just sets up a view environment variables and the path. I’m using a desktop shortcut because I want to put me in my Python project directory and set up some variables and the path.
Here are the options I have for the cmd.exe desktop shortcut.
Install the Terminal app from Microsoft and try Ctrl-S in it.
It’s free and developed in the open, code is on github.
The Terminal app is planned to replace the old cmd.exe console stuff.
Ok I downloaded the MS Terminal App, installed it and it opened a Powershell window by itself. Is Powershell and Terminal App the same thing? That’s confusing to have 2 different names.
If I have to use PS that’s fine, I’ll put the time into learning it. I just want to make sure I got the right thing.
It’s a terminal app that uses an existing shell program.
Terminal app runs powershell, cmd or a wsl shell.
It is not a replacement for the shell programs.
well, the await expression may fit (all in one interpreter, no shelling whatsoever)
import time
import asyncio
async def main(loops, seconds):
n = 0
while n < loops:
print(f'Message {n}')
await delay(seconds)
n += 1
async def delay(seconds):
start = time.perf_counter()
while (time.perf_counter() - start) < seconds:
pass # Do nothing, just wait
asyncio.run(main(5, 0.5))
or if coroutines are not your cup of tee use a closure (also state preserving)
import time
def main(loops, seconds):
def delay(seconds):
start = time.perf_counter()
while (time.perf_counter() - start) < seconds:
pass # Do nothing, just wait
return start
n = 0
while n < loops:
print(f'Message {n}')
print(delay(seconds))
n += 1
main(5, 0.5)