Thursday, November 21, 2013

Daemonic processes are not allowed to have children

You can use the multiprocessing module to 'farm' out a function to multiple cores using the Pool.map function. I was wondering idly if you can nest these farming operation to quickly build an exponentially growing army of processes to, you know, take over the world. It turns out that the universe has a failsafe:

import multiprocessing as mp

def compute(i):
  return i

def inner_pool(args):
  n0, N = args
  pool = mp.Pool()
  return pool.map(compute, range(n0,n0+N))

pool = mp.Pool()
print pool.map(inner_pool, [(n,n+10) for n in range(10)])

# -> AssertionError: daemonic processes are not allowed to have children

No comments:

Post a Comment