async_plugin.py 794 Bytes
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
from senpy import AnalysisPlugin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
2
3
4
5
6
7
8
9

import multiprocessing


def _train(process_number):
    return process_number


J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10
11
12
13
14
15
class Async(AnalysisPlugin):
    '''An example of an asynchronous module'''
    author = '@balkian'
    version = '0.2'
    async = True

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
    def _do_async(self, num_processes):
        pool = multiprocessing.Pool(processes=num_processes)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
18
        values = sorted(pool.map(_train, range(num_processes)))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
19
20
21
22
23
24
25
26
27
28
29

        return values

    def activate(self):
        self.value = self._do_async(4)

    def analyse_entry(self, entry, params):
        values = self._do_async(2)
        entry.async_values = values
        yield entry

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
30
31
32
33
34
35
36
37
    test_cases = [
        {
            'input': 'any',
            'expected': {
                'async_values': [0, 1]
            }
        }
    ]