Home Company Services Portfolio Contact us nav spacer
Home Page Feature

Nanosleep for python 2.4

by Izak Burger posted on Sep 18, 2008 10:47 AM last modified Apr 02, 2009 12:48 PM —

This is a short story about how I came to implement nanosleep for python 2.4.

This is a short story about how I came to implement nanosleep for python 2.4.

This is not a new idea, there are quite a few implementations of nanosleep using ctypes. The problem with these are that python2.4 does not include ctypes. It appears to be possible to add ctypes to any recent python but at the time this did not occur to me.

My need for a nanosleep implementation stems from a problem first noticed by Benyi York, documented here and here.

My implementation of nanosleep is supposed to be a drop-in replacement for time.sleep. You should be able to monkey-patch it into the application as follows:


import nanosleep
import time
time.sleep = nanosleep.nanosleep

I borrowed some code from a previous wrapper I created (with some help from old mailing list posts on the web) for uuidgen, and by borrowing code from timemodule.c in the python source code. The resulting source code can be downloaded here.

Please note that you may need to install the python development files if you compile this for your system python. For example, on debian you need to install python2.4-dev. To use it, you need to copy the resulting nanosleep.so file to a location in your PYTHONPATH, for example, /usr/local/lib/python2.4/site-packages.