Archive for the 'howto' Category

Java & Multicore

Recently I needed to run some very CPU intensive calculations in Java. In order to harvest the full power of my multicore machine I took a look at Java’s concurrent package. I never used this package before. Either I had some convenient third party library functions at my hand which successfully were able to hide all the multithreading stuff from me, sometimes I struggled with Thread, Runnable and synchronized statements or … I just used another language :P

So here are my results from a short trip into the adventurous realm of multicore programming in Java. To make things a little bit simpler in this post I will demonstrate to run several functions calculating a high Fibonacci number instead of using a more complicated real world example. The calculations will be distributed via a thread pool and all classes used in this example belong to the Java standard library, so there won’t be a need to install any extra packages.

Continue reading ‘Java & Multicore’

Advertisements

Struggling with HTML or How To Use Vertical And Horizontal Alignment In CSS

HTML wasn’t invented to write interactive, application-like web pages, it just offers a convenient way to create really simple documents containing links. Maybe that’s why programming web pages nowadays feels a little bit like writing a GUI in LaTeX. But maybe I am just complaining because I am not used to it … I am a software developer, not a web designer.

Continue reading ‘Struggling with HTML or How To Use Vertical And Horizontal Alignment In CSS’

Adding a Pidgin Trayicon to DWM

I am using dwm as a window manager for a long time. I hacked a few things into it which I felt were missing back then when I started to use dwm and got used to it so much that I hesitate to switch to awesome or even a newer version of dwm.

Tiled window managing is really a great idea but conflicts with programs which are design to be used in a more “traditional” desktop environment. One of those application is my instant messenger. I gave up using ncurses based messengers due to their lack of drag’n’drop capabilities, so I started using pidgin.

What I always wanted to have is some kind of tray icon which informs me about new incoming messages. The cool thing about pidgin and the whole underlying purple library is their great support for dbus. So instead of ripping off code from awesome to get full freedesktop compliant tray icons I wrote a small python script to add an icon to my dwm status bar.

The dwm status bar reads a string from stdin (passed to it by xinit) and displays it in the upper right corner of the screen. To do that it uses a loop in .xinitrc like this:

while true
do
	date=`date +'%d.%m.%Y %H:%M'`
	echo $date `pidginProbe.py`
	sleep 3
done | dwm

OK, so I need a script named pidginProbe.py which is just looking for an incoming message on the libpurple dbus interface, prints out a small notification and then terminates.

Here is how I connect to the purple dbus:

bus = dbus.SessionBus()
purple_service = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
purple = dbus.Interface(purple_service, "im.pidgin.purple.PurpleInterface")

Asking for an incoming message is not possible without installing a callback to a ReceivingChatMessage signal. But there is the option to ask for all active conversations which includes open chat windows and newly received messages.

def incomingMessageExists(purple):
    convs = purple.PurpleGetConversations()
    return len(convs) > 0

This function can be used to print [ ] to the screen when no message was received or [M] otherwise.

Of course much more is possible, nearly all important features can be used via dbus. See the developer documentation to find out about the other calls and signals.
I also added a notification which informs me about specific people going online (like my girlfriend ^^), here is the code example (that’s the simple version without error handling):

purple = dbus.Interface...
myid = # my ICQ/Jabber/... account id
contactid = # contact's ICQ/Jabber/... account id
myaccount = purple.PurpleAccountsFind(myid, '')
buddy = purple.PurpleFindBuddies(myaccount, contactid):
if purple.PurpleBuddyIsOnline(buddy[0]) == 1:
    print contactid, "is online"

Create New Classes With Python At Runtime

For a testing framework I want to create new classes in Python at runtime:

def mixIn(classname, parentclasses):
	if len(parentclasses) > 0:
		parents = map(lambda p:p.__name__, parentclasses)
		createclass = "class %s (%s):\n\tpass" % (classname, ",".join(parents))
	else:
		createclass = "class %s:\n\tpass" % classname
	exec createclass
	globals()[classname] = eval(classname)

This function creates a new class in the global namespace with the name classname inheriting from all classes in parentclasses, which is a list of strings.

Use the function like this:

class Foobar:
	def __init__(self, a):
		self.a = a
	def foo(self):
		return "foo" + str(self.a)

class Barfoo:
	def __init__(self, b, a):
		self.b = b
		self.a = a
	def bar(self):
		return "bar" + str(self.b) + str(self.a)

mixIn("Test", ["Foobar", "Barfoo"])
t = Test("23")
print t.foo() # this will print "foo23"
print t.bar() # this will throw an exception, because self.b is not defined

In most cases it’s not possible to generate a meaningful __init__ function automatically. The Python interpreter just takes the constructor of the first parent, but it’s possible to add another constructor afterwards.

def myinit(self, a, b):
	Foobar.__init__(self, a)
	Barfoo.__init__(self, b, a)
Test.__init__ = myinit
t = Test("23", "42")
print t.foo() # prints foo23
print t.bar() # prints bar4223

Java-like synchronized methods in C#

To prevent Threads using a method at the same time and messing things up Java programmers usually use the synchronized statement when declaring a method.

In the C# world lock-blocks can be used to archieve similar effects. But putting a whole function into a block looks nasty, another way is to use this method decorator:

[MethodImpl(MethodImplOptions.Synchronized)]

Please note, that System.Runtime.CompilerServices must be imported first.

How To Catch “Kill” Events with Python

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 API. The first one is to use Process.Kill which is the Microsoft version of SIGKILL and there is Process.CloseMainWindow which works similar to SIGTERM. To catch the latter one with Python you have to install the pywin32 library and define a handler by using win32api.SetControlCtrlHandler.

Here is a small example how to install an “exit handler” under Linux or Windows:

import os, sys
def set_exit_handler(func):
	if os.name == "nt":
		try:
			import win32api
			win32api.SetConsoleCtrlHandler(func, True)
		except ImportError:
			version = “.”.join(map(str, sys.version_info[:2]))
			raise Exception(”pywin32 not installed for Python ” + version)
	else:
		import signal
		signal.signal(signal.SIGTERM, func)

Use it like this:

if __name__ == "__main__":
	def on_exit(sig, func=None):
		print "exit handler triggered"
		import time
		time.sleep(5)
	set_exit_handler(on_exit)
	print "Press  to quit"
	raw_input()
	print "quit!"

Edit: I’ve cleaned up the code, this version should work with Python 2.4 and 2.6, I didn’t tried it with Python 3000 yet.

Displaying Disc Utilisation Information

Most Linux distributions come with a handy package called sysstat which is a collection of several different performance monitoring tools. To find processes which are consuming a lot of hard disc performance use pidstat:

 pidstat -dw

This will give you a list of all processes and their task switching activity (-w) which will include voluntary and non-voluntary context switches. Optionally you can also get I/O statistics (-d). Now look at the process you suspect to use much of the hard disc performance (i.e. grep for mysqld). A high number of voluntary context switches means, that the program waits for data to write or read (this includes all kind of data, also network data, so don’t misinterpret the numbers). If a process is constantly tries to write data to the hard disc but has a lot of voluntary context switches, the hard disc is probably overstressed which blocks the process and makes the scheduler switching to another application.
To see a summary of all I/O utilisation use:

  iostat -dmx 5

This will print every five seconds an extended (-x) utilisation statistic in megabytes (-m) about the current hard disc usage (-d). Look for the r/s or w/s column to figure out how much megabytes are read from/written to the disk per seconds.