Project

General

Profile

1
# Parallel processing
2

    
3
import cPickle
4
import itertools
5
import Queue
6
import rand
7
import types
8
import warnings
9

    
10
import collection
11
import dicts
12
import exc
13
from Runnable import Runnable
14

    
15
def try_pickle(value):
16
    try: cPickle.dumps(value)
17
    except Exception, e:
18
        exc.add_msg(e, 'Tried to pickle: '+repr(value))
19
        raise
20

    
21
def vars_id_dict(locals_, globals_, *misc):
22
    '''Usage: vars_id_dict(locals(), globals(), misc...)'''
23
    vars_ = map(lambda v: v.values(), [locals_, globals_]) + list(misc)
24
    return dicts.id_dict(vars_)
25

    
26
def prepickle(value, vars_id_dict_):
27
    def filter_(value):
28
        id_ = id(value)
29
        if id_ in vars_id_dict_: value = id_
30
        # Try pickling the value. If it fails, we'll get a full traceback here,
31
        # which is not provided with pickling errors in multiprocessing's Pool.
32
        else: try_pickle(value)
33
        return value
34
    return collection.rmap(filter_, value)
35

    
36
def post_unpickle(value, vars_id_dict_):
37
    def filter_(value):
38
        try: return vars_id_dict_[value] # value is an id()
39
        except KeyError: return value
40
    return collection.rmap(filter_, value)
41

    
42
class SyncPool:
43
    '''A dummy synchronous Pool to use if multiprocessing is not available'''
44
    def __init__(self, processes=None): pass
45
    
46
    class Result:
47
        def __init__(self, value): self.value = value
48
        
49
        def get(timeout=None): return self.value
50
        
51
        def wait(timeout=None): pass
52
        
53
        def ready(): return True
54
        
55
        def successful(): return True # TODO: False if raised exception
56
    
57
    def apply_async(self, func, args=(), kw_args={}, callback=None):
58
        if callback == None: callback = lambda v: None
59
        
60
        value = func(*args, **kw_args)
61
        callback(value)
62
        return self.Result(value)
63

    
64
class MultiProducerPool:
65
    '''A multi-producer pool. You must call pool.main_loop() in the thread that
66
    created this to process new tasks.'''
67
    
68
    def __init__(self, processes=None, locals_=None, globals_=None, *shared):
69
        '''
70
        @param processes If 0, uses SyncPool
71
        @post The # processes actually used is made available in self.process_ct
72
        '''
73
        if locals_ == None: locals_ = locals()
74
        if globals_ == None: globals_ = globals()
75
        
76
        try:
77
            if processes == 0: raise ImportError('turned off')
78
            import multiprocessing
79
            import multiprocessing.pool
80
        except ImportError, e:
81
            warnings.warn(UserWarning('Not using parallel processing: '+str(e)))
82
            processes = 1
83
            Pool_ = SyncPool
84
            Queue_ = Queue.Queue
85
        else:
86
            if processes == None: processes = multiprocessing.cpu_count()
87
            Pool_ = multiprocessing.pool.Pool
88
            Queue_ = multiprocessing.Queue
89
        
90
        self.process_ct = processes
91
        self.pool = Pool_(processes)
92
        self.queue = Queue_()
93
        # Values that may be pickled by id()
94
        self.vars_id_dict = vars_id_dict(locals_, globals_, *shared)
95
    
96
    def share(self, value):
97
        '''Call this on every value that that may be pickled by id()'''
98
        self.vars_id_dict[id(value)] = value
99
    
100
    def main_loop(self):
101
        try:
102
            while True:
103
                # block=False raises Empty immediately if the queue is empty,
104
                # which indicates that the program is done
105
                call = self.queue.get(block=False)
106
                self.pool.apply_async(call.func, self.post_unpickle(call.args),
107
                    self.post_unpickle(call.kw_args), call.callback)
108
        except Queue.Empty: pass
109
    
110
    class Result:
111
        def get(timeout=None): raise NotImplementedError()
112
        
113
        def wait(timeout=None): raise NotImplementedError()
114
        
115
        def ready(): raise NotImplementedError()
116
        
117
        def successful(): raise NotImplementedError()
118
    
119
    def apply_async(self, func, args=(), kw_args={}, callback=None):
120
        assert callback == None, 'Callbacks not supported'
121
        
122
        call = Runnable(func, *self.prepickle(args), **self.prepickle(kw_args))
123
        call.callback = callback # store this inside the Runnable
124
        
125
        self.queue.put_nowait(call)
126
        return self.Result()
127
    
128
    def prepickle(self, value): return prepickle(value, self.vars_id_dict)
129
    
130
    def post_unpickle(self, value):
131
        return post_unpickle(value, self.vars_id_dict)
(17-17/31)