|
1
|
|
|
import sys |
|
2
|
|
|
|
|
3
|
|
|
if sys.version_info < (3, 3): |
|
4
|
|
|
sys.stdout.write("Python 3.3 required\n") |
|
5
|
|
|
sys.exit(1) |
|
6
|
|
|
|
|
7
|
|
|
from multiprocessing import Process, Pipe |
|
8
|
|
|
from multiprocessing.connection import wait |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def chunk(seq, m): |
|
|
|
|
|
|
12
|
|
|
i, j, x = len(seq), 0, [] |
|
|
|
|
|
|
13
|
|
|
for k in range(m): |
|
14
|
|
|
a, j = j, j + (i + k) // m |
|
|
|
|
|
|
15
|
|
|
x.append(seq[a:j]) |
|
16
|
|
|
return x |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class PipePackage(): |
|
20
|
|
|
def __init__(self): |
|
21
|
|
|
self._current_process_id = None |
|
22
|
|
|
self._count_processes = 0 |
|
23
|
|
|
|
|
24
|
|
|
def setCurrentProcessId(self, current_process_id): |
|
|
|
|
|
|
25
|
|
|
self._current_process_id = current_process_id |
|
26
|
|
|
|
|
27
|
|
|
def setCountProcess(self, count): |
|
|
|
|
|
|
28
|
|
|
self._count_processes = count |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class KeepedAliveProcessManager(): |
|
32
|
|
|
def __init__(self, nb_process, target): |
|
33
|
|
|
self.processs = [] |
|
34
|
|
|
self.target = target |
|
35
|
|
|
self.nb_process = nb_process |
|
36
|
|
|
self.readers_pipes = [] |
|
37
|
|
|
self.writers_pipes = [] |
|
38
|
|
|
|
|
39
|
|
|
def _start(self, pipe_package): |
|
40
|
|
|
pipe_package.setCountProcess(self.nb_process) |
|
41
|
|
|
for i in range(self.nb_process): |
|
42
|
|
|
local_read_pipe, local_write_pipe = Pipe(duplex=False) |
|
43
|
|
|
process_read_pipe, process_write_pipe = Pipe(duplex=False) |
|
44
|
|
|
self.readers_pipes.append(local_read_pipe) |
|
45
|
|
|
self.writers_pipes.append(process_write_pipe) |
|
46
|
|
|
pipe_package.setCurrentProcessId(i) |
|
47
|
|
|
p = Process(target=run_keeped_process, |
|
|
|
|
|
|
48
|
|
|
args=(self.target, local_write_pipe, process_read_pipe, pipe_package)) |
|
49
|
|
|
p.start() |
|
50
|
|
|
self.processs.append(p) |
|
51
|
|
|
local_write_pipe.close() |
|
52
|
|
|
process_read_pipe.close() |
|
53
|
|
|
|
|
54
|
|
|
def stop(self): |
|
55
|
|
|
for writer_pipe in self.writers_pipes: |
|
56
|
|
|
writer_pipe.send('stop') |
|
57
|
|
|
|
|
58
|
|
|
def get_their_work(self, pipe_package): |
|
59
|
|
|
if not self.processs: |
|
60
|
|
|
self._start(pipe_package) |
|
61
|
|
|
else: |
|
62
|
|
|
for i in range(self.nb_process): |
|
63
|
|
|
# print('send things') |
|
64
|
|
|
pipe_package.setCurrentProcessId(i) |
|
65
|
|
|
self.writers_pipes[i].send(pipe_package) |
|
66
|
|
|
things_done_collection = [] |
|
67
|
|
|
reader_useds = [] |
|
68
|
|
|
while self.readers_pipes: |
|
69
|
|
|
for r in wait(self.readers_pipes): |
|
|
|
|
|
|
70
|
|
|
try: |
|
71
|
|
|
things_dones = r.recv() |
|
72
|
|
|
except EOFError: |
|
73
|
|
|
reader_useds.append(r) |
|
74
|
|
|
self.readers_pipes.remove(r) |
|
75
|
|
|
else: |
|
76
|
|
|
reader_useds.append(r) |
|
77
|
|
|
self.readers_pipes.remove(r) |
|
78
|
|
|
for thing_done in things_dones: |
|
79
|
|
|
things_done_collection.append(thing_done) |
|
80
|
|
|
|
|
81
|
|
|
self.readers_pipes = reader_useds |
|
82
|
|
|
return things_done_collection |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
def run_keeped_process(target, main_write_pipe, process_read_pipe, new_things): |
|
86
|
|
|
while new_things != 'stop': |
|
87
|
|
|
things_dones = target(new_things) |
|
88
|
|
|
main_write_pipe.send(things_dones) |
|
89
|
|
|
readers = [process_read_pipe] |
|
90
|
|
|
while readers: |
|
91
|
|
|
for r in wait(readers): |
|
|
|
|
|
|
92
|
|
|
try: |
|
93
|
|
|
new_things = r.recv() |
|
94
|
|
|
except EOFError: |
|
95
|
|
|
pass |
|
96
|
|
|
finally: |
|
97
|
|
|
readers.remove(r) |
|
98
|
|
|
|
|
99
|
|
|
# if new_things != 'stop': |
|
100
|
|
|
# run_keeped_process(target, main_write_pipe, process_read_pipe, new_things) |
|
101
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.