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
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.
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.
Related
I want to create a program that does something in which someone terminates the script by clicking the stop button in PyCharm. I tried
from sys import exit
def handler(signal_received, frame):
# Handle any cleanup here
print('SIGINT or CTRL-C detected. Exiting gracefully')
exit(0)
if __name__ == '__main__':
signal(SIGINT, handler)
print('Running. Press CTRL-C to exit.')
while True:
# Do nothing and hog CPU forever until SIGINT received.
pass
from https://www.devdungeon.com/content/python-catch-sigint-ctrl-c.
I tried on both Mac and Windows. On the Mac, PyCharm behaved as expected, when I click the stop button it catches the SIGINT. But on Windows, I did exactly the same thing, but it just straightly returns to me a
Process finished with exit code -1. Is there something I can do to change to make the Windows behave like what on Mac?
Any help is appreciated!
I don't think it's a strange question at all. On unix systems, pycham sends a SIGTERM, waits one second, then send a SIGKILL. On windows, it does something else to end the process, something that seems untrappable. Even during development you need a way to cleanly shut down a process that uses native resources. In my case, there is a CAN controller that, if not shut down properly, can't ever be opened again. My work around was to build a simple UI with a stop button that shuts the process down cleanly. The problem is, out of habit, from using pycharm, goland, and intellij, is to just hit the red, square button. Every time I do that I have to reboot the development system. So I think it is clearly also a development time question.
This actually isnt a simple thing, because PyCharm sends SIGKILL with the stop button. Check the discussion here https://youtrack.jetbrains.com/issue/PY-13316
There is a comment that you can enable "kill windows process softly", however it didnt work for me. The one that does work is emulate terminal in the debug config, then use control c when you select the console window
I wrote a Python script to detect a Print Screen keypress, and launch the Snipping Tool. The script uses subprocess.call to handle the application launch.
The problem I am facing is that when I am done with Snipping Tool and close the application, I get an "extra" application being launched. For example, if I took a snip of a PowerPoint presentation, then when I close Snipping Tool I get a new / blank instance of Power Point being launched automatically. I do not want this to happen, and just want the Snipping Tool to close without any additional action.
Can someone please help explain what I am missing in my code?
# https://stackoverflow.com/questions/24072790/detect-key-press-in-python
# https://pypi.org/project/keyboard/
# https://github.com/boppreh/keyboard#api
import keyboard #pip install keyboard
import time
import subprocess
while True:
if keyboard.is_pressed('print screen'):
subprocess.call(r'SnippingTool.exe') # blocking; waits until open
keyboard.press_and_release('ctrl+N')
#elif keyboard.is_pressed('ctrl+print screen'): # not recognizing "print screen" here
elif keyboard.is_pressed('ctrl+esc'):
print 'killing it now'
break
else:
time.sleep(0.1)
I'm guessing (and I'm not on Windows), but I think that the subprocess.call waits until you have finished with Snipping Tool, and so the keyboard.press_and_release('ctrl+N') is going to PowerPoint.
I wrote this piece of code and tried to debug it:
from time import *
for i in range (100):
sleep(1)
print(i)
I first run this script in debug mode, and try to pause it by clicking the pause button, but the pause button doesn't work at all, it just keep printing new numbers.
Then I directly run this script (not in the debug mode), the pause did stop pycharm from printing new numbers, but the script is actually still running in the background, when I resume the script, it prints a lot of numbers all of a sudden.
So how can I correctly pause the script execution?
I installed pycharm and python in a whole new windows 7, it still behaves like this.
The stop and rerun button works perfectly, breakpoints too. But the pause button never works.
The pause ("Pause Output") button only temporarily suspends output to the terminal - it has no effect on the script execution. You may wish to use debug mode with breakpoints instead.
You can add breakpoints into your program by clicking in the space to the left of the text editor (the "Left Gutter", where line numbers appear, if you have them enabled).
See the Pycharm documentation for more information.
Update 2022
We now have a pause button in debug mode :
While debugging my Python 3.5 pogram in Pycharm 5.0.4, I am trying to hit the pause button to find how why/where the program is hanging (as can be done in Visual Studio).
However, nothing happens: the pause button does not become grey and the resume button stays grey, and in the debugger tool window, "Frames are not available".
I tried with different basic programs, on Linux and on Windows, to no avail.
Is this a bug or am I missing something in how Pycharm debugging is supposed to work?
I also noticed that when a breakpoint is hit, only one thread is suspended and I could see no way to suspend other threads to inspect their stack frames. I would be interested to know how to achieve this thread-specific suspension as well.
Sounds like your program is hanging on a sleep or something of that sort, or maybe on some native code.
If it was a regular python loop the pause python would work.
I believe the problem is with python itself and not the debug tool you are using.
When you pause a python program you pause the interperter and so all threads that are running in the context of the interperter are paused and you can see the them in the frames window. Any thread that show the message "Frames not available in non-suspended state" is not suspended because it is was sleeping when you paused the program.
see this for how to debug c code
Not working python breakpoints in C thread in pycharm or eclipse+pydev
Within PyCharm, there is an option for debugging, that will allow you to step through your code, which may be of more use, rather than trying to pause the program.
You need to insert a break point in the code initially; just click in the grey bar at the line you want to break at:
Then you can press Alt+Shift+F9, or click Run > Debug in the menu to start stepping through the code from that point:
Once you have started the debug mode, click the button highlighted by the red circle - this will enable you to step through the code, looking at the variables, their assignments and if you receive any errors.
If you need to stop at any point, just hit the red Stop button on the left of the debug window.
The console tab will allow you to see what is being printed to the screen (if anything), and at what point, save you having loads of print statements like you would if debugging using Idle or similar IDEs
HTH
I've seen some apps allow you to show/hide the console when you need to read log messages. For example Blender3D allows that (blender.org).
I was wondering if this can be done in Python and how.
My main window is a Panda3D (panda3d.org) window.
I've read somewhere that one option is to hide the "real" console (pythonw) and create another console and just redirect everything from the "real" one to it, every time you want to "show" the "real" console. No idea how this can be done.
Or at least a way to choose whether to start the program with the console or without it by reading a configuration file or something.
I'm assuming you are talking about Windows because this console toggling in blender is Windows exclusive. I'm guessing Blender uses GetConsoleWindow and ShowWindow on Windows.
This is how you could do it in python with pywin32:
import win32gui, win32console, win32api, win32con
import time
console_window = win32console.GetConsoleWindow()
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_HIDE)
time.sleep(1)
win32gui.ShowWindow(console_window, win32con.SW_SHOW)
time.sleep(1)
If you run this program with python and not pythonw it will show the console, sleep for a second, hide the console, sleep for another second and then hide it again.
Mind that this code only works on Windows. On other platforms silly stuff like this is not necessary because if you want a program to show a console then you run it from the console.