Project

General

Profile

1 1858 aaronmk
# Parallel processing
2
3 1873 aaronmk
import cPickle
4 1877 aaronmk
import itertools
5 1862 aaronmk
import Queue
6 1877 aaronmk
import rand
7 1873 aaronmk
import types
8 1858 aaronmk
import warnings
9
10 1877 aaronmk
import collection
11
import dicts
12 1873 aaronmk
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 1877 aaronmk
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 1873 aaronmk
26 1877 aaronmk
def prepickle(value, vars_id_dict_):
27
    def filter_(value):
28 1880 aaronmk
        id_ = id(value)
29
        if id_ in vars_id_dict_: value = id_
30 1877 aaronmk
        # 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 1880 aaronmk
        else: try_pickle(value)
33 1877 aaronmk
        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 1862 aaronmk
class SyncPool:
43 1861 aaronmk
    '''A dummy synchronous Pool to use if multiprocessing is not available'''
44 1862 aaronmk
    def __init__(self, processes=None): pass
45
46 1860 aaronmk
    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 1877 aaronmk
    def apply_async(self, func, args=(), kw_args={}, callback=None):
58 1858 aaronmk
        if callback == None: callback = lambda v: None
59
60 1860 aaronmk
        value = func(*args, **kw_args)
61
        callback(value)
62 1863 aaronmk
        return self.Result(value)
63 1858 aaronmk
64 1862 aaronmk
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 1873 aaronmk
68 1877 aaronmk
    def __init__(self, processes=None, locals_=None, globals_=None, *shared):
69 1862 aaronmk
        '''
70
        @param processes If 0, uses SyncPool
71
        @post The # processes actually used is made available in self.process_ct
72
        '''
73 1877 aaronmk
        if locals_ == None: locals_ = locals()
74
        if globals_ == None: globals_ = globals()
75
76 1862 aaronmk
        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 1877 aaronmk
        # Values that may be pickled by id()
94
        self.vars_id_dict = vars_id_dict(locals_, globals_, *shared)
95 1862 aaronmk
96 1877 aaronmk
    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 1862 aaronmk
    def main_loop(self):
101
        try:
102 1873 aaronmk
            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 1877 aaronmk
                self.pool.apply_async(call.func, self.post_unpickle(call.args),
107
                    self.post_unpickle(call.kw_args), call.callback)
108 1862 aaronmk
        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 1877 aaronmk
    def apply_async(self, func, args=(), kw_args={}, callback=None):
120
        assert callback == None, 'Callbacks not supported'
121 1873 aaronmk
122 1877 aaronmk
        call = Runnable(func, *self.prepickle(args), **self.prepickle(kw_args))
123 1873 aaronmk
        call.callback = callback # store this inside the Runnable
124
125 1862 aaronmk
        self.queue.put_nowait(call)
126 1863 aaronmk
        return self.Result()
127 1877 aaronmk
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)