TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: ALL
from: R.WIESER
date: 2019-12-16 19:59:00
subject: Re: Python & thonny - how

Dennis,

> How would you "terminate the script graciously (run some cleaning-up
> code)" if you ran your script from the command line/shell?

Under DOS/Windows ?  By checking if a certain key was being pressed, and if
so exit the loop, allowing the (cleanup) code following the loop to be
executed.  But as this is Linux I haven't got the foggiest.

> Most IDEs run scripts by spawning a process and connecting stdin/
> stdout to that process (so they can pass keystrokes to the process
> and capture output for display in some console window of the IDE).

I know.   That is why I specifically mentioned Thonny.  It could be pushing
the keystrokes into the stdin of the Python program, but have no idea if it
actually does that.

> I suspect that "stop" button is the equivalent of sending a 
> to the running process

If I would not have found something that uses that very key combination to
enable me to exit a loop I would most likely have agreed with you. :-)

The code in the below traps SIGINT, which seems to be generated by pressing
ctrl-c.    Thonny's "stop" button does not get trapped by it.

https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in-python

> The most common would be to wrap your script in an exception
> handler, trapping KeyboardInterrupt

I had that in some example code downloaded, and I could not get the script
to terminate that way.  But as I wanted to make absolutily sure I just now
tried some minimal code.  And ofcourse /now/ it works.  :-\

So, now I've got two methods to choose from. :-)

Regards,
Rudy Wieser

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.