Python signal handling

Signals are one of the most under estimated tools on POSIX systems. Windows is supposed to also handle (some) signals, but I've never used it and don't plan to either. These notes are therefor based on Linux and similar behaving systems.

I used signal processing a lot in Perl coding but since I'm still learning Python I thought it might be a good idea to check it out. As it turns out, it's extremely easy in Python.

For this sample app I wanted to know how easy it would be to tell the app the re-read it's config. The app doesn't really do anything useful but it may perhaps be used for a skeleton of some sorts. Here goes:


import sys
import signal
import time

sleeptime = 5.0 # Every how many seconds do we need to perform hosekeeping
house_tracker = 0 # Track the last time we did housekeeping

def SigUSR1Handler(signum, frame):
global house_tracker
house_tracker = time.time() # Reset the timer of the last house keeping
print "Reacting on USR1 signal (signal 10)"

def ReadConfig():
print str( time.time() ) + "\tRead Config"

def Housekeeping( timestamp, interval=10 ):
global house_tracker # Last time we did house cleaning
if ( timestamp - house_tracker ) > interval:
print str( timestamp ) + " - Housekeeping timer reached. Running house cleaning routines"
house_tracker = timestamp

def HouseKeepingRoutines():
# Put all the housekeeping functions here
ReadConfig( )

signal.signal( signal.SIGUSR1, SigUSR1Handler )

# Set initial timers
start_time = time.time()
previous_time = start_time
abs_starttime = start_time
time_now = time.time()
if ( time_now - previous_time ) > sleeptime:
previous_time = time_now
running_time = time_now - abs_starttime
print "Uptime: '" + str( running_time ) + "' seconds"
Housekeeping( time_now )
time.sleep( 1 )

It may look complicated at first glance but it's really not. The app behaves like a "server" that needs to do some house keeping from time to time. When it starts up all timers start fresh and the house keeping routines are called immediately. Part of the house keeping is to read (or re-read) the configuration file - which in this sample just prints a message to STDOUT.

But, if you run the app and you get it's process ID, you can send it a SIGUSR1 signal and you will see from the output it re-read it's config. To issue the command:

$ kill -10 23456

That's assuming of course your app is running with process id 23456.

"But why" you may wonder?

Well, for one thing you can tweak the configuration parameters and in stead of a restart you can tell your app just to re-read and apply the new configuration parameters. A good example could be that you need to change DB servers to do maintenance on your primary. With this method you could force your app to drop all current DB connections and re-establish the connection with the new parameters. Of course you will have to write some extra code - but I'm sure you understand the concept.

Being able to make changes in a production environment without customer impact is sort of a holy grail for most IT shops. This gets you one step closer.


Labels: , ,



Python: what day is tomorrow?

Easy yes? Well - sort of. It's easy if you want to know tomorrows date. It's not so easy if you want to work out the next date from "any" other date.

This is by no means perfect - it's work in progress, but for now it works for me:

The simple solution to calculate tomorrow (from the Python Cookbook):

import datetime
today = datetime.date.today( )
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)
print yesterday, today, tomorrow

The harder one - calculating tomorrow from any other date:

import datetime
import re
somedate = "2007-02-28"
sd_elements = re.split( "-", str( somedate ) )
d = datetime.datetime( int( sd_elements[0] ), int( sd_elements[1] ), int( sd_elements[2] ) )
tomorrow = d + datetime.timedelta(days=1)
m = re.search( '(\d\d\d\d-\d\d-\d\d)', str( tomorrow ), re.IGNORECASE )
tomorrow = str( m.group(1) )
print "If today is " + str( somedate ) + " then tomorrow must be " +str( tomorrow )

Like I said - most likely not perfect.




Difference between Windows 7 32bit and 64bit

Interesting to see there's about 600MB+ difference between the two ISO images... What gives?



Getting VMWare keyboard to play nicely in Ubuntu

I always found it a bit annoying and finally got the stage where I could no longer take it! If you use VMWare server/workstation on Ubuntu you may find that the arrow keys (amongst others) do not work as expected. For example, one of the arrow keys will pop-up the start menu if your guest OS is Windows.

Lucky for Ubuntu users the fix is simple:
        echo 'xkeymap.nokeycodeMap = true' > ~/.vmware/config

All you need to do to complete the fix is to restart the vmware service. Next time you start-up and log into your guest OS your keys will all work as expected.

Labels: , ,



Windows 7

From Screenshots
So I finally installed Windows 7. It's a relatively old beta build - but it appears to be running smoothly.

For those interested, Microsoft has the RC1 available until somewhere in July 2009. It will work full functional until 1 March 2010 at which time it will shut down after 2 hours.

Now to see what works and what breaks :-)

This page is powered by Blogger. Isn't yours?