Terminating python script and releasing resources

Python script can be used as a background running service in order to provide some functionality. This service is often stopped on very aggressive way with process termination. Therefore some resources may not be released or child threads stopped.Below are two options how to handle script termination.

1.  Catching KeyboardInterrupt and SystemExit errors:

 except (KeyboardInterrupt, SystemExit):
     print("Termination occurred")


2.  Register on system signals:

Described in this post:  How To Catch “Kill” Events with Python


How To Catch “Kill” Events with Python

daniel's devel blog

Currently I’m working on a small MRCPv2 client written in Python, which is part of a bigger project at my laboratory. The client is started remotely by a program which controls and monitors several applications in the whole network and is stopped by it again.

In the Linux world catching “kill events” would be a simple task, to shut down another program a process just has to send a SIGTERM signal which can be caught be Python easily using the signal module which comes with every Python installation. However, on Windows things are a little bit harder, because the operating system never produces SIGTERM (and SIGILL) events according to the signal.h documentation from MSDN. That’s why it took me some time to figure out how to catch “kill” events under Windows with Python.

First of all, there are two different ways to close a program in Windows using the .NET…

View original post 87 more words


Raspberry PI: Volume control

Set the volume of sound output jack or USB sound card on Raspberry PI with Raspbian distro.

This action will be perfomed with amixer utility.

First list all audio capable devices:

$ amixer

Sample output:

Simple mixer control 'Speaker',0
 Capabilities: pvolume pswitch pswitch-joined penum
 Playback channels: Front Left - Front Right
 Limits: Playback 0 - 151
 Front Left: Playback 151 [100%] [0.06dB] [on]
 Front Right: Playback 151 [100%] [0.06dB] [on]
Simple mixer control 'Mic',0
 Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined penum
 Playback channels: Mono
 Capture channels: Mono
 Limits: Playback 0 - 127 Capture 0 - 16
 Mono: Playback 0 [0%] [0.00dB] [off] Capture 0 [0%] [0.00dB] [on]
Simple mixer control 'Auto Gain Control',0
 Capabilities: pswitch pswitch-joined penum
 Playback channels: Mono
 Mono: Playback [on]

In my case I have three diferent control options. Try to find the right one. I am using external USB dongle with stereo output, so I will use “Speaker,0” device to control.

Execute command to set volume of selected control to 100% (or use prefered value):

$ amixer sset Speaker,0 100%

So simple is that. 🙂