def __init__(self, processes=None, initializer=None, initargs=(), maxtasksperchild=None): if not callable(initializer): raise TypeError('initializer must be a callable') # this workaround is needed b/c initargs can (and do) contain an amp.SharedQueue, # which can not be marshalled, but COW will deal with it properly by binding it # into this local 'packaged_initializer' def packaged_initializer(initializer=initializer, initargs=initargs): return initializer(*initargs) self.packaged_count += 1 self._initializer = '_amp_pool_%s_%d' % (initializer.__name__, self.packaged_count) import __main__ setattr(__main__, self._initializer, packaged_initializer) self._group = amp.ProcessGroup(processes) global _process_groups _process_groups.append(self._group)
def test03Instantiations( self ): """Test class instantiations""" import _athenamp proc = _athenamp.Process( -1 ) self.assertEqual( proc.pid, -1 ) proc = _athenamp.Process( pid = -1 ) self.assertEqual( proc.pid, -1 ) group = _athenamp.ProcessGroup() group = _athenamp.ProcessGroup( 4 ) group = _athenamp.ProcessGroup( nprocs = 4 ) queue = _athenamp.SharedQueue() queue = _athenamp.SharedQueue( 100 )
def test02RunMapAsync( self ): """Test no-op running of map_async on a worker group""" import _athenamp group = _athenamp.ProcessGroup( 4 ) group.map_async( "exit" ) self.assertEqual( len(group._children()), 4 ) # now instantiated status = group.wait() self.assertEqual( [ x[1] for x in status ], 4*[0,] )
def test01GroupLifetime( self ): """Test creation and life time of a group""" import _athenamp group = _athenamp.ProcessGroup( 4 ) # nothing started yet, so waiting should simply return self.assertEqual( group.wait(), () ) self.assertEqual( group.wait( 0 ), () ) self.assertEqual( group.wait( options = 0 ), () )
def test03PythonTaskMapAsync( self ): """Test running a python task via map_async on a worker group""" import _athenamp, __main__ def myfunc(): # print 'called myfunc' return 1 __main__.myfunc = myfunc # existing function with return value group = _athenamp.ProcessGroup( 4 ) group.map_async( "myfunc" ) status = group.wait() self.assertEqual( [ x[1] for x in status ], 4*[0,] ) self.assertEqual( [ x[2] for x in status ], 4*[1,] ) # non-existing function, leading to failure group = _athenamp.ProcessGroup( 4 ) group.map_async( "no_such_func" ) status = group.wait() self.assertEqual( [ x[1] for x in status ], 4*[0x0B,] )