astropy/astropy — pull request #974 by mdboom (2013-04-12 18:30:06+00:00)
Title: units: don't play nice with multiprocessing

This one hurts...just rewrote a large chunk of code to use `astropy.units` and then find this :sweat:

Anyway, here's some code that works fine on a single process (e.g., copy and paste into interpreter):

import astropy.units as u
import numpy as np

def some_func(r):
return r.to(u.kpc)

print(map(some_func, [np.random.random()*u.km for ii in range(16)]))

Now try it with multiprocessing:

import astropy.units as u
import numpy as np
import multiprocessing

def some_func(r):
return r.to(u.kpc)

pool = multiprocessing.Pool(processes=4)
print(pool.map(some_func, [np.random.random()*u.km for ii in range(16)]))

and you get:

ERROR: UnitsException: 'km' (length) and 'kpc' (length) are not convertible [multiprocessing.pool]
---------------------------------------------------------------------------
UnitsException Traceback (most recent call last)
in ()
8
9 pool = multiprocessing.Pool(processes=4)
---> 10 print(pool.map(some_func, [np.random.random()*u.km for ii in range(16)]))

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.pyc in map(self, func, iterable, chunksize)
197 '''
198 assert self._state == RUN
--> 199 return self.map_async(func, iterable, chunksize).get()
200
201 def imap(self, func, iterable, chunksize=1):

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.pyc in get(self, timeout)
489 return self._value
490 else:
--> 491 raise self._value
492
493 def _set(self, i, obj):

UnitsException: 'km' (length) and 'kpc' (length) are not convertible