/
mproc.py
53 lines (42 loc) · 1.37 KB
/
mproc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Copyright (c) 2015 by Tobias Houska
This file is part of Statistical Parameter Estimation Tool (SPOTPY).
:author: Philipp Kraft
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import pathos.multiprocessing as mp
class PhaseChange(object):
"""
Object to identify a change of a simulation phase
"""
def __init__(self,phase):
self.phase=phase
class ForEach(object):
"""
ForEach is a classes for multiprocessed work based on a generater object which is given if __call__ is called
We using the pathos multiprocessing module and the orderd map function where results are saved until results in
the given order are caluculated. We yielding back the result so a generator object is created.
"""
def __init__(self,process):
self.size = mp.cpu_count()
self.process = process
self.phase=None
self.pool = mp.ProcessingPool(self.size)
def is_idle(self):
return False
def terminate(self):
pass
def start(self):
pass
def setphase(self,phasename):
self.phase=phasename
def f(self, job):
data = self.process(job)
return data
def __call__(self,jobs):
results = self.pool.imap(self.f, jobs)
for i in results:
yield i