Fix bug in Python SWIG layer.

The Python interpreter can shut down threading before we are finished with
it unless an explicit lock is taken.

See:
http://stackoverflow.com/questions/12178897/swig-crashes-python