Пример #1
0
 def run(self):
     self.sample = test_uniform_threaded(
         self.sample_size, self.wait_time)
Пример #2
0
    def _generate_samples_in_parallel(self,
                                      n_task=10,
                                      n_workers=None,
                                      wait_time=0,
                                      parallelization_type='multiprocessing'):
        """This function generates samples of uniform distribution in parallel

        Parameters
        ----------
        n_task : `int`
            Number of task that will be run

        n_workers : `int`
            Number of workers that will be used.
            If it is set to None the maximum number of workers will be used.
            Otherwise this number of processes will be created (in case of
            multiprocessing) or work will be executed sequestially (in case
            of threading)

        wait_time : `float`
            Time that will be waited on each thread after random number
            simulation

        parallelization_type : `string`
            How work will be parallelized : either using multiprocessing or
            using threading
        """
        sample_size = self.stat_size
        args = [(sample_size, wait_time) for _ in range(n_task)]

        if parallelization_type == 'multiprocessing':

            pool = Pool(processes=n_workers)
            samples = pool.starmap(test_uniform_threaded, args)

            samples = np.array(samples)

        elif parallelization_type == 'threading':

            class UniformThread(threading.Thread):
                def __init__(self, sample_size, wait_time):
                    super(UniformThread, self).__init__()
                    self.sample_size = sample_size
                    self.wait_time = wait_time
                    self.sample = None

                def run(self):
                    self.sample = test_uniform_threaded(
                        self.sample_size, self.wait_time)

            if n_workers is None:
                threads = []
                for arg in args:
                    threads.append(UniformThread(*arg))

                [t.start() for t in threads]
                [t.join() for t in threads]
                samples = np.array([t.sample for t in threads])

            else:
                samples = np.array(
                    [test_uniform_threaded(*arg) for arg in args])
        else:
            raise ValueError('Unknown thread type')

        return samples