How to use the multiprocessing.pool.ThreadPool function in multiprocessing

To help you get started, we’ve selected a few multiprocessing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CSAILVision / NetDissect / src / labelprobe.py View on Github external
def fast_process(fn_t, fn_read, shape, tally_depth, ds, iw, ih,
        categories, fieldmap,
        thresh, labelcat, batch_size, ahead, verbose, parallel):
    psize = int(numpy.ceil(float(ds.size()) / parallel))
    ranges = [(s, min(ds.size(), s + psize))
            for s in range(0, ds.size(), psize) if s < ds.size()]
    parallel = len(ranges)
    original_sigint_handler = setup_sigint()
    # pool = multiprocessing.Pool(processes=parallel, initializer=setup_sigint)
    pool = multiprocessing.pool.ThreadPool(processes=parallel)
    restore_sigint(original_sigint_handler)
    # Precache memmaped files
    blobdata = cached_memmap(fn_read, mode='r', dtype='float32',
            shape=shape)
    count_t = cached_memmap(fn_t, mode='r+', dtype='int32',
        shape=(ds.size(), tally_depth, 3))
    data = [
        (fn_t, fn_read, shape, tally_depth, ds, iw, ih, categories, fieldmap,
            thresh, labelcat, batch_size, ahead, verbose, True) + r
        for r in ranges]
    try:
        result = pool.map_async(individual_process, data)
        result.get(31536000)
    except KeyboardInterrupt:
        print("Caught KeyboardInterrupt, terminating workers")
        pool.terminate()
github mapsme / omim / tools / python / maps_generator / maps_generator.py View on Github external
def build_world(country):
        stage_index(env, country)
        stage_cities_ids_world(env, country)
        env.finish_mwm(country)

    def build_world_coasts(country):
        stage_index(env, country)
        env.finish_mwm(country)

    specific = {
        WORLD_NAME: build_world,
        WORLD_COASTS_NAME: build_world_coasts
    }

    mwms = env.get_mwm_names()
    with ThreadPool() as pool:
        pool.map(lambda c: specific[c](c) if c in specific else build(c), mwms,
                 chunksize=1)
github datasnakes / OrthoEvolution / OrthoEvol / Tools / ftp / ncbiftp.py View on Github external
files2download)

        # Move to directory for file downloads
        os.chdir(download_path)

        # Download the files using multiprocessing
        download_time_secs = time()
        with ThreadPool(1) as download_pool:
            download_pool.map(self.download_file, files2download)
            minutes = round(((time() - download_time_secs) / 60), 2)
        self.ncbiftp_log.info("Took %s minutes to download the files." %
                              minutes)

        if extract:
            extract_time_secs = time()
            with ThreadPool(1) as extract_pool:
                extract_pool.map(self.extract_file, files2download)
                minutes = round(((time() - extract_time_secs) / 60), 2)
            self.ncbiftp_log.info("Took %s minutes to extract from all files." %
                                  minutes)
github GoogleCloudPlatform / marketplace-k8s-app-tools / scripts / doctor.py View on Github external
else:
      fn = v
      prerequisites = None
      extra_args = None
    tasks[task_name] = Task(
        name=task_name,
        function=fn,
        prerequisites=set(prerequisites or []),
        args=extra_args or {})

  # Dry-run to ensure valid DAG.
  do_run(
      tasks, lambda task, event_queue: event_queue.put(
          TaskEvent(name=task.name, success=True)))

  with ThreadPool(5) as pool:
    return do_run(
        tasks, lambda task, event_queue: pool.apply_async(
            execute_task, args=(task, event_queue)))
github VarIr / scikit-hubness / skhubness / neighbors / lsh.py View on Github external
# Allocate memory for neighbor indices (and distances)
        n_objects = X.shape[0]
        neigh_ind = np.empty((n_objects, n_candidates), dtype=np.int32)
        if return_distance:
            neigh_dist = np.empty_like(neigh_ind, dtype=X.dtype)

        # If verbose, show progress bar on the search loop
        disable_tqdm = False if self.verbose else True

        if self.n_jobs > 1:
            def pquery(ix):
                i, x = ix
                return i, np.array(query.find_k_nearest_neighbors(x, k=n_retrieve))

            with mp.pool.ThreadPool(processes=self.n_jobs) as pool:
                i_knn = list(tqdm(pool.imap_unordered(func=pquery,
                                                      iterable=enumerate(X),
                                                      chunksize=10),
                                  disable=False if self.verbose else True,
                                  total=X.shape[0],
                                  unit='vectors',
                                  desc='LSH query'))
                for i, knn in tqdm(i_knn, desc='Collecting results', disable=disable_tqdm):
                    if query_is_train:
                        knn = knn[1:]
                    neigh_ind[i, :knn.size] = knn

                    if return_distance:
                        neigh_dist[i, :knn.size] = distances(X[i].reshape(1, -1), self.X_train_[knn])

                    # LSH may yield fewer neighbors than n_neighbors.
github vipshop / TupleNet / src / tuplenet / tools / pkt-trace.py View on Github external
def run_pkt_trace_async(inject_info_list):
    pool = ThreadPool(processes = THREAD_POOL_MAX_N)
    async_result_list = []
    for inject_port, packet in inject_info_list:
        async_result_list.append(pool.apply_async(run_pkt_trace,
                                                  (inject_port, packet)))
    result = []
    for async_result in async_result_list:
        result.append(async_result.get())
    return result
github lrbsunday / distsuper / distsuper / common / tools.py View on Github external
else:
            waste_time = round(time.time() - start_time, 2)
            logger.debug("[%s][%s/%s][%s:%s][%ss] process succeed" % (
                func.__name__, i, s,
                pid, tid, waste_time))
            return ret

    if to_random:
        random.shuffle(arg_list_list)

    total = len(list(arg_list_list))
    arg_list_list = [[i + 1, total] + arg_list for i, arg_list in
                     enumerate(arg_list_list)]
    logger.info('@@ deal %s %s tasks with %s threads pool' % (
        total, func.__name__, t_num))
    pool = ThreadPool(processes=t_num)
    results = pool.map(__process, arg_list_list, chunksize=1)
    logger.info('@@ all %s %s tasks done' % (total, func.__name__))
    return results
github onitu / onitu / onitu / plug / dealer.py View on Github external
def __init__(self, plug):
        super(Dealer, self).__init__()
        self.plug = plug
        self.name = plug.name
        self.escalator = plug.escalator
        self.logger = Logger(u"{} - Dealer".format(self.name))
        self.context = plug.context
        self.in_progress = {}
        self.pool = ThreadPool()
github V0RT3X4 / python-sdk / vortexasdk / client.py View on Github external
def _process_multiple_pages(
        self, total: int, url: str, payload: Dict, data: Dict
    ) -> List:
        size = data.get("size", 1000)
        offsets = list(range(0, total, size))
        shuffle(offsets)

        with tqdm(
            total=total, desc="Loading from API", disable=(len(offsets) == 1)
        ) as pbar:
            with ThreadPool(self._N_THREADS) as pool:
                logger.info(
                    f"{total} Results to retrieve."
                    f" Sending {len(offsets)}"
                    f" post requests in parallel using {self._N_THREADS} threads."
                )

                func = functools.partial(
                    _send_post_request_data,
                    url=url,
                    payload=payload,
                    size=size,
                    progress_bar=pbar,
                )

                return pool.map(func, offsets)
github Kent-Lee / artstation-scraper / lib / artstation.py View on Github external
def save_artist(self, artist_id, dir_path):
        artist_name = self.artist(artist_id)["name"]
        print(f"download for artist {artist_name} begins\n")
        dir_path = utils.make_dir(dir_path, artist_id)
        artworks = self.artist_artworks(artist_id, dir_path)
        if not artworks:
            print(f"artist {artist_name} is up-to-date\n")
            return
        with ThreadPool(self.threads) as pool:
            files = pool.map(partial(self.save_artwork, dir_path), artworks)
        print(f"\ndownload for artist {artist_name} completed\n")
        combined_files = utils.counter(files)
        utils.file_mtimes(combined_files["names"], dir_path)
        return combined_files