I have been writing a program in python using OpenCV. Up to this point, I had not set the mouse callback (cv2.setMouseCallback). To exit the program (which is in fullscreen), I would press the ESC key (Line 70).
I recently added a mouse callback (Line 11) which works as it should, however, now when I press the ESC key, the program does not terminate as it had previously. The while loop finishes, and cv2.destroyAllWindows() and sys.exit(0) are called. The window does close, and no python code after sys.exit(0) is executed, however no prompt is returned in Command Prompt (in which the python program was started).
My first thought was that there was a thread running that had not been stopped, however I have no threads in my code, and the thread that calls the onMouse function (Line 50) is the same as the main loop thread (i.e. it would not appear that opencv has a seperate thread for mouse callbacks).
My code can be found here: http://pastie.org/9246511
I am stumped, and any help is greatly appreciated.
Please note: You will need a webcam plugged in to test the code
Your code seems to exit correctly running it as a script from a prompt and running in Pycharm when using sys.exit().
If you are running it in Ipython you need to use exit() to return to the command prompt:
Just use exit() after cv2.destroyAllWindows() and it will terminate.
In [1]: type(exit)
Out[1]: IPython.core.autocall.ExitAutocall
Just incase you never got the answer...I just had the same issue. Turns out it was because in my my config file (Run/Debug Configurations) I had checked the "Show command line afterwards" box. Once I unchecked that the window was killed correctly.
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 am running a python project in pycharm. In the code we have a main "try-catch-finally" block e.g.
try:
# Some stuff like opening files and video streams
except SomePossibleExceptions:
# Handle possible exception
finally:
# Save, close and tidy up unfinished files / videos / output streams
If I run the program in the terminal it will reach the finally block when I press our quit button or "ctrl-c" and perform the post processing required. However, after pressing "stop" when using the run tool in PyCharm it just quits and does not reach the finally block.
The obvious answer is to just run in the terminal but is there a way to get PyCharm to run the finally block after pressing "stop" in the run tool?
No, using the red stop button terminates the process immediately.
I think you alluded to this, but the only workaround I have found is to edit the run configuration:
run > edit configurations > tick the box for 'Emulate terminal in output console' or 'Run with Python Console'.
With either of those selections ticked, the run window will accept ctrl-C inputs which will allow your finally block to execute.
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.
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 :
i write some python program.
i usually execute my program by CMD
but this time, i tried to execute program by double click
it works well until interpreter meet input code.
when i input some texts, it shut down
my input code is
for i in (input('range input => ')).split(' '):
range_list.append(int(i));
it works totally well when i execute by path(py ~.py) through the CMD
can you help me?
The interpreter is running in an endless loop. Executing your program from windows or via the command line using python will run and exit the program immediately.
At the end of your program just add
input()
This will keep it open so you can see your results.
Yeah when the program is done, it closes.
You can add something like x = input() at the end if you want to keep it open, or just run it in cmd.
Your program opens in a windowed mode when you double click it. The window will disappear / close down when the program finishes, which happens a lot fast after you have entered all the required inputs.
If you wish to see the output before the program exit, expect an input at the end of your program.