Project

General

Profile

« Previous | Next » 

Revision 1858

Added parallel.py for parallel processing

View differences:

lib/parallel.py
1
# Parallel processing
2

  
3
import warnings
4

  
5
class Pool:
6
    def apply_async(func, args=None, kw_args=None, callback=None):
7
        if args == None: args = ()
8
        if kwds == None: kwds = {}
9
        if callback == None: callback = lambda v: None
10
        
11
        callback(func(*args, **kw_args))
12

  
13
def mk_pool(cpus=None):
14
    '''@return tuple(pool, cpus)'''
15
    try:
16
        if cpus == 0: raise ImportError('Parallel processing turned off')
17
        import multiprocessing
18
        import multiprocessing.pool
19
    except ImportError, e:
20
        warnings.warn(UserWarning('Not using parallel processing: '+str(e)))
21
        pool = Pool()
22
    else:
23
        if cpus == None: cpus = multiprocessing.cpu_count()
24
        pool = multiprocessing.pool.Pool(processes=cpus)
25
    return (pool, cpus)

Also available in: Unified diff