Beware of threaded Python Mapnik renders

Maybe this will save someone time/confusion…

I wasn’t having much luck with TileMill etc. so I wrote a simple Python script to render a Mapnik XML file to PNGs of different resolutions.

So, say I am rendering the following resolutions:

resolutions = [0.125, 0.25, 0.5, 1, 2, 4]

They output individual files, named like this:


    * my-map-0.125.png
    * my-map-0.25.png
    * my-map-[...].png
    * my-map-4.png

The images are embedded in a HTML page which uses JavaScript to refresh them periodically. I am using watchmedo to run the script every time the XML file is changed. Crude but fairly effective… or at least it was until I used the threading and Queue libraries to try and speed things up.

Examples of issues:

  1. Sometimes files will have the wrong resolution rendered
  2. Sometimes Min/Max Scale Denominator is not respected

There may be other issues but these are the only ones I have notice so far. It seems that scale/resolution values are not isolated between the threads in my pool of four.

I do not know whether this is a problem with Mapnik, just the Python bindings or a problem with how “multithreading” is implemented in CPython. I going to restructure the code so that each render uses its own process.

For the record I am using Python 3.5 and Mapnik 3.0.