當前位置: 首頁>>代碼示例>>Python>>正文


Python process.ProcessPoolExecutor方法代碼示例

本文整理匯總了Python中concurrent.futures.process.ProcessPoolExecutor方法的典型用法代碼示例。如果您正苦於以下問題:Python process.ProcessPoolExecutor方法的具體用法?Python process.ProcessPoolExecutor怎麽用?Python process.ProcessPoolExecutor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在concurrent.futures.process的用法示例。


在下文中一共展示了process.ProcessPoolExecutor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: main

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def main():
    urls = [
        'https://talkpython.fm',
        'https://pythonbytes.fm',
        'https://google.com',
        'https://realpython.com',
        'https://training.talkpython.fm/',
    ]

    work = []

    with PoolExecutor() as executor:
        for url in urls:
            # print("Getting title from {}".format(url.replace('https', '')),
            #       end='... ',
            #       flush=True)
            # title = get_title(url)
            f: Future = executor.submit(get_title, url)
            work.append(f)

        print("Waiting for downloads...", flush=True)

    print("Done", flush=True)
    for f in work:
        print("{}".format(f.result()), flush=True) 
開發者ID:talkpython,項目名稱:async-techniques-python-course,代碼行數:27,代碼來源:program.py

示例2: musicSpider

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def musicSpider():
    print("======= 開始爬 音樂 信息 ===========")
    startTime = datetime.datetime.now()
    print(startTime.strftime('%Y-%m-%d %H:%M:%S'))
    # 所有專輯數量
    albums_num = sql.get_all_album_num()
    print("所有專輯數量:", albums_num)
    # 批次
    batch = math.ceil(albums_num.get('num') / 1000.0)
    # 構建線程池
    # pool = ProcessPoolExecutor(1)
    for index in range(0, batch):
        saveMusicBatch(index)
        # pool.submit(saveMusicBatch, index)
    # pool.shutdown(wait=True)
    print("======= 結束爬 音樂 信息 ===========")
    endTime = datetime.datetime.now()
    print(endTime.strftime('%Y-%m-%d %H:%M:%S'))
    print("耗時:", (endTime - startTime).seconds, "秒")

# if __name__ == '__main__':
#     musicSpider() 
開發者ID:NacedWang,項目名稱:163MusicSpider,代碼行數:24,代碼來源:music_by_album.py

示例3: lyricSpider

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def lyricSpider():
    print("======= 開始爬 歌詞 信息 ===========")
    startTime = datetime.datetime.now()
    print(startTime.strftime('%Y-%m-%d %H:%M:%S'))
    # 所有歌手數量
    musics_num = sql.get_all_music_num()
    # 批次
    batch = math.ceil(musics_num.get('num') / 1000.0)
    # 構建線程池
    # pool = ProcessPoolExecutor(1)
    for index in range(0, batch):
        saveLyricBatch(index)
        # pool.submit(saveLyricBatch, index)
    # pool.shutdown(wait=True)
    print("======= 結束爬 歌詞 信息 ===========")
    endTime = datetime.datetime.now()
    print(endTime.strftime('%Y-%m-%d %H:%M:%S'))
    print("耗時:", (endTime - startTime).seconds, "秒")

# if __name__ == '__main__':
#     lyricSpider() 
開發者ID:NacedWang,項目名稱:163MusicSpider,代碼行數:23,代碼來源:lyric_by_music.py

示例4: commentSpider

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def commentSpider():
    print("======= 開始爬 評論 信息 ===========")
    startTime = datetime.datetime.now()
    print(startTime.strftime('%Y-%m-%d %H:%M:%S'))
    # 所有歌手數量
    musics_num = sql.get_all_music_num()
    # 批次
    batch = math.ceil(musics_num.get('num') / 1000.0)
    # 構建線程池
    # pool = ProcessPoolExecutor(1)
    for index in range(0, batch):
        saveCommentBatch(index)
        # pool.submit(saveCommentBatch, index)
    # pool.shutdown(wait=True)
    print("======= 結束爬 評論 信息 ===========")
    endTime = datetime.datetime.now()
    print(endTime.strftime('%Y-%m-%d %H:%M:%S'))
    print("耗時:", (endTime - startTime).seconds, "秒")

# if __name__ == '__main__':
#     commentSpider() 
開發者ID:NacedWang,項目名稱:163MusicSpider,代碼行數:23,代碼來源:comments_by_music.py

示例5: test_init_cpu

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def test_init_cpu(dist_init_method):
    args = argparse.Namespace(
        multiprocessing_distributed=True,
        dist_world_size=2,
        dist_rank=None,
        ngpu=0,
        local_rank=None,
        dist_launcher=None,
        distributed=True,
        dist_backend="gloo",
        dist_init_method=dist_init_method,
        dist_master_addr=None,
        dist_master_port=None,
    )
    args.dist_rank = 0
    option = build_dataclass(DistributedOption, args)
    args.dist_rank = 1
    option2 = build_dataclass(DistributedOption, args)
    with ProcessPoolExecutor(max_workers=2) as e:
        fn = e.submit(option.init)
        fn2 = e.submit(option2.init)
        fn.result()
        fn2.result() 
開發者ID:espnet,項目名稱:espnet,代碼行數:25,代碼來源:test_distributed_utils.py

示例6: test_init_cpu2

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def test_init_cpu2():
    args = argparse.Namespace(
        multiprocessing_distributed=True,
        dist_world_size=2,
        dist_rank=None,
        ngpu=0,
        local_rank=None,
        dist_launcher=None,
        distributed=True,
        dist_backend="gloo",
        dist_init_method="env://",
        dist_master_addr=None,
        dist_master_port=free_port(),
    )
    args.dist_rank = 0
    option = build_dataclass(DistributedOption, args)
    args.dist_rank = 1
    option2 = build_dataclass(DistributedOption, args)
    with ProcessPoolExecutor(max_workers=2) as e:
        fn = e.submit(option.init)
        fn2 = e.submit(option2.init)
        with pytest.raises(RuntimeError):
            fn.result()
            fn2.result() 
開發者ID:espnet,項目名稱:espnet,代碼行數:26,代碼來源:test_distributed_utils.py

示例7: test_init_cpu4

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def test_init_cpu4():
    args = argparse.Namespace(
        multiprocessing_distributed=True,
        dist_world_size=2,
        dist_rank=None,
        ngpu=0,
        local_rank=None,
        dist_launcher=None,
        distributed=True,
        dist_backend="gloo",
        dist_init_method="env://",
        dist_master_addr="localhost",
        dist_master_port=free_port(),
    )
    args.dist_rank = 0
    option = build_dataclass(DistributedOption, args)
    args.dist_rank = 1
    option2 = build_dataclass(DistributedOption, args)
    with ProcessPoolExecutor(max_workers=2) as e:
        fn = e.submit(option.init)
        fn2 = e.submit(option2.init)
        fn.result()
        fn2.result() 
開發者ID:espnet,項目名稱:espnet,代碼行數:25,代碼來源:test_distributed_utils.py

示例8: test_init_cpu5

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def test_init_cpu5():
    args = argparse.Namespace(
        multiprocessing_distributed=True,
        dist_world_size=2,
        dist_rank=None,
        ngpu=0,
        local_rank=None,
        dist_launcher=None,
        distributed=True,
        dist_backend="gloo",
        dist_init_method="env://",
        dist_master_addr="localhost",
        dist_master_port=free_port(),
    )
    args.dist_rank = 0
    option = build_dataclass(DistributedOption, args)
    args.dist_rank = 1
    option2 = build_dataclass(DistributedOption, args)
    with ProcessPoolExecutor(max_workers=2) as e:
        fn = e.submit(option.init)
        fn2 = e.submit(option2.init)
        fn.result()
        fn2.result() 
開發者ID:espnet,項目名稱:espnet,代碼行數:25,代碼來源:test_distributed_utils.py

示例9: process_all

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def process_all(self, texts: Collection[str]) -> List[List[str]]:
        "Process a list of `texts`."
        if self.n_cpus <= 1:
            return self._process_all_1(texts)
        with ProcessPoolExecutor(self.n_cpus) as e:
            return sum(
                e.map(self._process_all_1, partition_by_cores(texts, self.n_cpus)), []
            ) 
開發者ID:jrzaurin,項目名稱:pytorch-widedeep,代碼行數:10,代碼來源:fastai_transforms.py

示例10: __init__

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def __init__(self, num_workers):
        '''
            Create a process pool for concurrent execution with specified number of workers.
        '''
        self.executor_pool = ProcessPoolExecutor(num_workers) 
開發者ID:tanwanirahul,項目名稱:django-batch-requests,代碼行數:7,代碼來源:executor.py

示例11: __init__

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def __init__(self, executor: ProcessPoolExecutor):
        self._executor = executor
        self._loop = asyncio.get_event_loop() 
開發者ID:vmagamedov,項目名稱:grpclib,代碼行數:5,代碼來源:server.py

示例12: main

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def main(*, host: str = '127.0.0.1', port: int = 50051) -> None:
    with ProcessPoolExecutor(max_workers=4) as executor:
        server = Server([Primes(executor)])
        with graceful_exit([server]):
            await server.start(host, port)
            print(f'Serving on {host}:{port}')
            await server.wait_closed() 
開發者ID:vmagamedov,項目名稱:grpclib,代碼行數:9,代碼來源:server.py

示例13: test_resolve_distributed_mode_slurm3

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def test_resolve_distributed_mode_slurm3():
    args = argparse.Namespace(
        multiprocessing_distributed=True,
        dist_world_size=None,
        dist_rank=None,
        ngpu=1,
        local_rank=None,
        dist_launcher="slurm",
        dist_backend="nccl",
        dist_init_method="env://",
        dist_master_addr=None,
        dist_master_port=10000,
    )
    env = dict(
        SLURM_PROCID="0",
        SLURM_NTASKS="1",
        SLURM_STEP_NUM_NODES="1",
        SLURM_STEP_NODELIST="localhost",
        SLURM_NODEID="0",
        CUDA_VISIBLE_DEVICES="0,1",
    )

    e = ProcessPoolExecutor(max_workers=2)
    with unittest.mock.patch.dict("os.environ", dict(env, SLURM_LOCALID="0")):
        resolve_distributed_mode(args)
        option = build_dataclass(DistributedOption, args)
        fn = e.submit(option.init)

    with unittest.mock.patch.dict("os.environ", dict(env, SLURM_LOCALID="0")):
        option2 = build_dataclass(DistributedOption, args)
        fn2 = e.submit(option2.init)

    fn.result()
    fn2.result() 
開發者ID:espnet,項目名稱:espnet,代碼行數:36,代碼來源:test_distributed_utils.py

示例14: __init__

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def __init__(self, max_workers=None):
        if max_workers is None:
            max_workers = _utils.get_optimal_process_count()
        super(ProcessPoolExecutor, self).__init__(max_workers=max_workers)
        if self._max_workers <= 0:
            raise ValueError("Max workers must be greater than zero")
        self._gatherer = _Gatherer(
            # Since our submit will use this gatherer we have to reference
            # the parent submit, bound to this instance (which is what we
            # really want to use anyway).
            super(ProcessPoolExecutor, self).submit,
            self.threading.lock_object) 
開發者ID:openstack,項目名稱:futurist,代碼行數:14,代碼來源:_futures.py

示例15: _set_executor_pool

# 需要導入模塊: from concurrent.futures import process [as 別名]
# 或者: from concurrent.futures.process import ProcessPoolExecutor [as 別名]
def _set_executor_pool(self) -> None:
        """Set thread pool pool to be used."""
        self._executor_pool = ProcessPoolExecutor(max_workers=len(self._tasks)) 
開發者ID:fetchai,項目名稱:agents-aea,代碼行數:5,代碼來源:multiple_executor.py


注:本文中的concurrent.futures.process.ProcessPoolExecutor方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。