01 September 2009

Threadsafety and matplotlib/pylab

Matplotlib and pylab are two different interfaces to the same (very useful) python plotting library. Pylab is the easier-to-use and more succinct of the two, but, unfortunately, it relies on global state which makes it most definitely _not_ threadsafe. If you try to use it on a multithreaded web application, you'll get graphs overlaid on top of each other, random changes to your graph parameters, and all sorts of other nasties that don't really bear mentioning.

You can avoid this by switching to matplotlib's Figure()-based api. This thread has a nice example. (Note that the thread is actually about a failure of matplotlib to work multithreadedly. But it sounds like an obscure one.)

No comments:

Post a Comment

About Me

blog at barillari dot org Older posts at http://barillari.org/blog