How to pause debugging program in a loop?

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.

Thank you.

Ctrl-S/Ctrl-Q works for me in Windows 11 using the Microsoft Terminal application from the App store.

The AI suggestions do not make sense.

well, here is VS code


:joy:

I’m looking into other methods to sleep, like this from Gemini AI which I haven’t tried yet. It accepts decimal seconds which I use a lot.

import time

def delay(seconds):
    start = time.perf_counter()
    while (time.perf_counter() - start) < seconds:
        pass  # Do nothing, just wait

print("Message 1")
delay(0.5)  # Wait for 0.5 seconds
print("Message 2")

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 on Windows 10. I will try the Terminal app.

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.

And the Terminal tab.

Under “Default terminal application” there are no entries to choose.

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.

1 Like

Thank you. I find the Terminal App here. Windows Terminal - Free download and install on Windows | Microsoft Store

I’ll install it and try it out.

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.

You can configure terminal to open cmd.exe or powershell.exe as well as wsl prompts.

well, that doesn’t sound all that good, does it ?

That 1 star review is not true. Not sure why you posted it…

You can start a cli app from either cmd or powershell in terminal app.
Since I don’t use powershell I just tested to be absolutely sure.

I didn’t post any review on the Powershell/Terminal app. That was on the site when I found the web page.

well, since it isn’t an MS app I looked around (like I do in Amazon) and newest first gave me this :cowboy_hat_face:

Your screen shot clearly shows it is from Microsoft Corporation!

ok, Open Source-Project, in VS is also plenty of beta, but I doubt it can keep up with the PowerShell :smirk:

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.

1 Like

well, the await expression may fit (all in one interpreter, no shelling whatsoever) :rofl:

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) :nerd_face:

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)

Ok I have time to address this issue now. If I run a python program in the pdb debugger in cmd.exe, CTRL-C and CTRL-S do not work most of the time, at least they never work immediately.

So AFAIK, MS Terminal App is not a thing, it either runs Powershell or cmd.exe or wsl. When I search for “Terminal” on my Windows start menu I can see it has a sub menu and it can open cmd.exe, Powershell, Azure shell (which I installed in the past), Developer Command Prompt for VSC 2022, and Developer Powershell for VSC 2022.

When I just click on “Terminal” on my Start menu it opens Powershell by default.

  1. So am I just stuck with Powershell?
  2. In PS I couldn’t even find the path to Python with echo %PATH%. And when running a Python program which ran fine in cmd.exe I got a lot of errors which I will not post here.

WSL just allows me to install various Linux systems without booting into Linux, which I don’t want. We don’t use Linux. The Powers What Is have determined we are a Microsoft shop.

I may make another thread about running a Python program in Powershell.

Thanks.

You have to install Terminal from the microsoft store app.
You will love it!