Python Single Process Pool
Yep, this sounds a bit strange - a single process pool? why would you need this??
What happened to me is that I wrote several scripts that use the (excellent) multiprocessing package for the very easy creation of a process pool.
from multiprocessing import Pool
The thing is that on some platforms, it does not run well.Â
Not sure why is that, but I get an Issue 3770 (which should be closed http://bugs.python.org/issue3770) on a virtual machine, which basically means that I cannot use multiple processes using the process pool. Since I just want the same code to work on this vm, I wrote a simple class that mimics some of the functionality of the Pool:
class SingleApplyResult(object):
     def __init__(self,v):
         self.value=v
     def get(self):
         return self.value
class SingleProcessPool(object):
     def __init__(self):
         pass
     def apply_async(self, func, args=(), kwds={}, callback=None):
         value = func(*args, **kwds)
         return SingleApplyResult(value)
     def close(self):
         pass
     def join(self):
         pass
def get_pool( processes, force_singlethread=False ):
     try:
         if force_singlethread:
             pool=SingleProcessPool()
         else:
             pool=Pool(processes=processes)
     except:
         print "failed getting multi-process pool, getting single-process"
         pool=SingleThreadPool()
     return pool
That's it. When I want to create the pool, I just write:
pool = get_pool(processes=20)
and it gives me a pool which either supports many processes, or just one if the platform does not support it.
I only implemented the methods that I use (apply_async, close and join), but it's very easy to extend.










