1

I am building a command line tools using Python script. it's a loop to check data and print out some stuff after some delay seconds. It works fine until I click anything or selecting text by mouse on the terminal without keyboard event. it doesn't do anything after that, doesn't print and recheck

import time
import sys
print('some thing')
for remaining in range(10, 0, -1):
    sys.stdout.write("\r")
    sys.stdout.write("recheck in {:2d}.".format(remaining))
    sys.stdout.flush()
    time.sleep(1)

sys.stdout.write("\rComplete!            \n")
input()

My environment is anaconda 64bit on windows 10

michael
  • 21
  • 7
  • I tried it on mac osx terminal and iterm. I could not reproduce your failure. I tried clicking, selecting text, and hammering on the keyboard. – SargeATM Oct 02 '18 at 04:34
  • 1
    Could your clicking be suspending the process in your terminal somehow? – SargeATM Oct 02 '18 at 04:37
  • Sorry, my environment is anaconda 64bit on windows 10, it seems just freezing and when I press any key on the keyboard, it continues to run and print out, don't know why, just found, time.sleep can be interrupted by a signal – michael Oct 02 '18 at 06:18
  • Found that time.sleep() can be interrupted by a signal but not sure did it appear on my case and how to stop it – michael Oct 02 '18 at 17:45
  • Are you starting this from cmd terminal or anaconda gui environment? – SargeATM Oct 02 '18 at 18:07
  • @michael I would suggest editing this information into your post. Comments aren't intended to be permanent. – Tyberius Oct 02 '18 at 18:08
  • @SargeATM for both, and double click on the script to run it too, no way work fine – michael Oct 03 '18 at 10:53
  • @Tyberius sorry, thank you – michael Oct 03 '18 at 10:53

2 Answers2

2

The console is blocking in the Windows SDK function WriteConsole because the console window is in a mode called QuickEdit mode.

To fix the issue, go to the properties option in the upper left corner menu of the console.

Then uncheck QuickEdit mode.

QuickEdit mode is there to help with copying and pasting text from the console. So when the console is in that mode, it stops all writing to the console so that the text isn't moving while you are trying to select and copy/paste.

SargeATM
  • 2,483
  • 14
  • 24
  • thanks a lot, finally I found the problem, it related to it [QuickEdit mode in Command Prompt freeze applications?](https://stackoverflow.com/questions/30418886/how-and-why-does-quickedit-mode-in-command-prompt-freeze-applications) But I still stuck in here, if I disable the Quick edit mode then I can't copy/paste on the console that I want to do when using the script. Do you have any suggestions? – michael Oct 04 '18 at 02:52
  • Build a simple GUI with Tk using the python tkinter module with a status textbox and a results textbox, and an execute button. – SargeATM Oct 04 '18 at 03:29
  • Thank you so much, I think I have to try that solution – michael Oct 07 '18 at 03:05
1

Python significantly changed its system signal handling in Python 3.5. https://www.python.org/dev/peps/pep-0475/

It used to throw an InterruptedError whenever a signal interrupted a system call. Now the system call wrapper code upon signal interruption will recall the system call recalculating any timeouts if necessary. A bug at this level could recall the system call with an absurdly long value.

Attach a debugger and see where the process is at when it is stuck.

EDIT: after attaching windbg to stuck console. I discovered that this isn't the problem. I posted the real solution in a new answer.

SargeATM
  • 2,483
  • 14
  • 24