Example #1
0
def graph(name, values):
    proc, parent, legend = [], [], []
    for v in values:
        p, c = Pipe()
        parent.append(p)
        switch = {
            "main": (c, default_generations, default_population,
                     default_crossover, default_mutation),
            "generation":
            (c, v, default_population, default_crossover, default_mutation),
            "population":
            (c, default_generations, v, default_crossover, default_mutation),
            "crossover":
            (c, default_generations, default_population, v, default_mutation),
            "mutation":
            (c, default_generations, default_population, default_crossover, v)
        }
        proc.append(Process(target=genetic_algorithm, args=switch.get(name)))
        legend.append("{} = {}".format(name, v))
    for p in proc:
        p.start()
    for p in proc:
        p.join()
    for p in parent:
        plot.plot(p.recv())
    if values[0] is not None:
        plot.legend(legend, loc='lower right')
    plot.ylabel("Fitness")
    plot.xlabel("Generation")
    plot.savefig("{}.png".format(name), bbox_inches='tight')
    plot.close()
Example #2
0
    def run(self):
        try:
            signal.signal(signal.SIGTERM, self.stop)

            self._is_running = Value('b', True)

            self._processes = []
            self._collector_pipes = []

            for proc_num in range(self._cfg['workers_num']):
                p, c = Pipe()

                self._collector_pipes.append(p)

                self._processes.append(
                    Collector(
                        name='snoopy-collector-col',
                        pipe=c,
                        proc_num=proc_num,
                        rabbit_cfg=self._cfg['rabbit_cfg'],
                        rabbit_sc_cfg=self._cfg['rabbit_snoopy_charges_cfg'],
                        is_running=self._is_running
                    )
                )

            self._processes.append(
                Director(
                    name='snoopy-collector-dir',
                    pipes=self._collector_pipes,
                    cfg=self._cfg,
                    is_running=self._is_running
                )
            )

            [p.start() for p in self._processes]

            self._logger.info('I\'m ready!')

            while self._is_running.value:
                try:
                    signal.pause()
                except:
                    continue

            [p.join() for p in self._processes]

            self._logger.info('Finalised shutdown.')

        except:
            self._logger.exception('General failure')
Example #3
0
            else:
                print('index:{}, something wrong!'.format(c))
        except EOFError as e:
            print("connection had broken")

        if stop.is_set():
            print('consumer over')
            end.close()
            break


p, q = Pipe()

# p.recv()
p = Process(target=producer, args=(p, ))
p.daemon = True
p.start()

q = Process(target=consumer, args=(q, ))
q.daemon = True
q.start()

c = 10
while c:
    c -= 1
    time.sleep(0.2)
stop.set()

p.join()
q.join()