[GRASS-dev] Python scripts: trigger cleanup function at keyboard interrupt (CTRL-C)

Markus Neteler neteler at osgeo.org
Mon Oct 14 08:15:34 PDT 2019

On Mon, Oct 14, 2019 at 5:04 PM Moritz Lennert
<mlennert at club.worldonline.be> wrote:
> On 14/10/19 16:44, Anna Petrášová wrote:
> > This is used in many scripts and should work:
> > https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Sophisticated_cleanup_procedure

Thanks for the link (now I remember again).

> Not sure this works on a Ctrl-C. ISTR it doesn't, but this would have to
> be verified.

It doesn't:

## LL location:
# random command
r.in.srtm.region -1 user="Neteler4520" password="13Heymel. at nasa"
output=srtm_sicily_1arc memory=2000 --o
Importing 4 SRTM tiles...
^C^C^C 100%
^C^CTraceback (most recent call last):
  File "/home/mneteler/.grass7/addons/scripts/r.in.srtm.region", line
480, in <module>
  File "/home/mneteler/.grass7/addons/scripts/r.in.srtm.region", line
406, in main
    quiet = True)
  File "/home/mneteler/software/grass78_git/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
line 496, in read_command
    stdout, stderr = process.communicate()
  File "/usr/lib64/python3.7/subprocess.py", line 926, in communicate
    stdout = self.stdout.read()
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib64/python3.7/subprocess.py", line 990, in wait
    return self._wait(timeout=timeout)
  File "/usr/lib64/python3.7/subprocess.py", line 1624, in _wait
    (pid, sts) = self._try_wait(0)
  File "/usr/lib64/python3.7/subprocess.py", line 1582, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)

The function atexit() is used:

grep atexit $HOME/.grass7/addons/scripts/r.in.srtm.region
import atexit

We discussed this in March 2018, but yet without result:

pygrass: How to allow Ctrl-C to stop all subprocesses of GridModule
... and following messages.


More information about the grass-dev mailing list