def _test():
 import thread
 import time
 def f(x):
  return (x * x)
 def work(seconds):
  print ('[%d] Start to work for %fs...' % (thread.get_ident(), seconds))
  time.sleep(seconds)
  print ('[%d] Work done (%fs).' % (thread.get_ident(), seconds))
  return ('%d slept %fs' % (thread.get_ident(), seconds))
 pool = Pool(9)
 result = pool.apply_async(f, (10,))
 print result.get(timeout=1)
 print pool.map(f, range(10))
 it = pool.imap(f, range(10))
 print it.next()
 print it.next()
 print it.next(timeout=1)
 result = pool.apply_async(time.sleep, (3,))
 try:
  print result.get(timeout=1)
 except TimeoutError:
  print 'Good. Got expected timeout exception.'
 else:
  assert False, 'Expected exception !'
 print result.get()
 def cb(s):
  print ('Result ready: %s' % s)
 for res in pool.imap(work, xrange(10, 3, (-1)), chunksize=4):
  print 'Item:', res
 for res in pool.imap_unordered(work, xrange(10, 3, (-1))):
  print 'Item:', res
 result = pool.map_async(work, xrange(10), callback=cb)
 try:
  print result.get(timeout=1)
 except TimeoutError:
  print 'Good. Got expected timeout exception.'
 else:
  assert False, 'Expected exception !'
 print result.get()
 result = pool.imap_async(work, xrange(3, 10), callback=cb)
 try:
  print result.get(timeout=1)
 except TimeoutError:
  print 'Good. Got expected timeout exception.'
 else:
  assert False, 'Expected exception !'
 for i in result.get():
  print 'Item:', i
 print '### Loop again:'
 for i in result.get():
  print 'Item2:', i
 result = pool.imap_unordered_async(work, xrange(10, 3, (-1)), callback=cb)
 try:
  print result.get(timeout=1)
 except TimeoutError:
  print 'Good. Got expected timeout exception.'
 else:
  assert False, 'Expected exception !'
 for i in result.get():
  print 'Item1:', i
 for i in result.get():
  print 'Item2:', i
 r = result.get()
 for i in r:
  print 'Item3:', i
 for i in r:
  print 'Item4:', i
 for i in r:
  print 'Item5:', i
 result = pool.imap_unordered_async(work, xrange(2, (-10), (-1)), callback=cb)
 time.sleep(3)
 try:
  for i in result.get():
   print 'Got item:', i
 except IOError:
  print 'Good. Got expected exception:'
  traceback.print_exc()
 result = pool.imap_async(work, xrange(2, (-10), (-1)), callback=cb)
 time.sleep(3)
 try:
  for i in result.get():
   print 'Got item:', i
 except IOError:
  print 'Good. Got expected exception:'
  traceback.print_exc()
 pool.terminate()
 print 'End of tests'

def _test ( ) : import thread import time def f ( x ) : return ( x * x ) def work ( seconds ) : print ( '[%d] Start to work for %fs...' % ( thread . get_ident ( ) , seconds ) ) time . sleep ( seconds ) print ( '[%d] Work done (%fs).' % ( thread . get_ident ( ) , seconds ) ) return ( '%d slept %fs' % ( thread . get_ident ( ) , seconds ) ) pool = Pool ( 9 ) result = pool . apply_async ( f , ( 10 , ) ) print result . get ( timeout = 1 ) print pool . map ( f , range ( 10 ) ) it = pool . imap ( f , range ( 10 ) ) print it . next ( ) print it . next ( ) print it . next ( timeout = 1 ) result = pool . apply_async ( time . sleep , ( 3 , ) ) try : print result . get ( timeout = 1 ) except TimeoutError : print 'Good. Got expected timeout exception.' else : assert False , 'Expected exception !' print result . get ( ) def cb ( s ) : print ( 'Result ready: %s' % s ) for res in pool . imap ( work , xrange ( 10 , 3 , ( - 1 ) ) , chunksize = 4 ) : print 'Item:' , res for res in pool . imap_unordered ( work , xrange ( 10 , 3 , ( - 1 ) ) ) : print 'Item:' , res result = pool . map_async ( work , xrange ( 10 ) , callback = cb ) try : print result . get ( timeout = 1 ) except TimeoutError : print 'Good. Got expected timeout exception.' else : assert False , 'Expected exception !' print result . get ( ) result = pool . imap_async ( work , xrange ( 3 , 10 ) , callback = cb ) try : print result . get ( timeout = 1 ) except TimeoutError : print 'Good. Got expected timeout exception.' else : assert False , 'Expected exception !' for i in result . get ( ) : print 'Item:' , i print '### Loop again:' for i in result . get ( ) : print 'Item2:' , i result = pool . imap_unordered_async ( work , xrange ( 10 , 3 , ( - 1 ) ) , callback = cb ) try : print result . get ( timeout = 1 ) except TimeoutError : print 'Good. Got expected timeout exception.' else : assert False , 'Expected exception !' for i in result . get ( ) : print 'Item1:' , i for i in result . get ( ) : print 'Item2:' , i r = result . get ( ) for i in r : print 'Item3:' , i for i in r : print 'Item4:' , i for i in r : print 'Item5:' , i result = pool . imap_unordered_async ( work , xrange ( 2 , ( - 10 ) , ( - 1 ) ) , callback = cb ) time . sleep ( 3 ) try : for i in result . get ( ) : print 'Got item:' , i except IOError : print 'Good. Got expected exception:' traceback . print_exc ( ) result = pool . imap_async ( work , xrange ( 2 , ( - 10 ) , ( - 1 ) ) , callback = cb ) time . sleep ( 3 ) try : for i in result . get ( ) : print 'Got item:' , i except IOError : print 'Good. Got expected exception:' traceback . print_exc ( ) pool . terminate ( ) print 'End of tests'
a function that exists for test purposes .