婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python concurrent.futures模塊的使用測試

python concurrent.futures模塊的使用測試

熱門標簽:電銷機器人錄音要學習什么 如何地圖標注公司 煙臺電話外呼營銷系統 銀川電話機器人電話 企業彩鈴地圖標注 預覽式外呼系統 外賣地址有什么地圖標注 長春極信防封電銷卡批發 上海正規的外呼系統最新報價

概述

concurrent.futures 是 3.2 中引入的新模塊,它為異步執行可調用對象提供了高層接口。
可以使用 ThreadPoolExecutor 來進行多線程編程,ProcessPoolExecutor 進行多進程編程,兩者實現了同樣的接口,這些接口由抽象類 Executor 定義。
這個模塊提供了兩大類型,一個是執行器類 Executor,另一個是 Future 類。
執行器用來管理工作池,future 用來管理工作計算出來的結果,通常不用直接操作 future 對象,因為有豐富的 API。

說明

Python3.2開始,標準庫為我們提供了concurrent.futures模塊,它提供了ThreadPoolExecutor和ProcessPoolExecutor兩個類,實現了對threading和multiprocessing的進一步抽象,對編寫線程池/進程池提供了直接的支持.

#! /usr/bin/env python
# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         demo3
# Author:       yunhgu
# Date:         2021/7/8 15:17
# Description: 
# -------------------------------------------------------------------------------
import os
import time
import threading
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed

def work(x):
    time.sleep(1)
    temp = f"父進程{os.getppid()}:子進程{os.getpid()}:線程{threading.get_ident()}:{x}"
    return temp

def sub_thread():
    temp_list = []
    with ThreadPoolExecutor(max_workers=3) as t:
        task_list = [t.submit(work, i) for i in range(5)]
        for task in as_completed(task_list):
            if task.done():
                temp_list.append(task.result())
    return temp_list

def main():
    print(f"主進程:{os.getpid()}")
    path_list = []
    with ProcessPoolExecutor(max_workers=3) as p:
        task_list = [p.submit(sub_thread) for i in range(5)]
        for task in as_completed(task_list):
            if task.done():
                path_list.append(task.result())
    for path in path_list:
        print(path)

if __name__ == '__main__':
    main()

不論你在什么時候開始,重要的是開始之后就不要停止。不論你在什么時候結束,重要的是結束之后就不要悔恨。

到此這篇關于python concurrent.futures模塊的使用測試 的文章就介紹到這了,更多相關python concurrent使用內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python基于concurrent模塊實現多線程
  • python 實現多進程日志輪轉ConcurrentLogHandler
  • Python并發concurrent.futures和asyncio實例
  • Python concurrent.futures模塊使用實例
  • python程序中的線程操作 concurrent模塊使用詳解

標簽:宜昌 盤錦 珠海 潮州 西寧 佳木斯 上饒 湖北

巨人網絡通訊聲明:本文標題《python concurrent.futures模塊的使用測試》,本文關鍵詞  python,concurrent.futures,模塊,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python concurrent.futures模塊的使用測試》相關的同類信息!
  • 本頁收集關于python concurrent.futures模塊的使用測試的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 马鞍山市| 凤冈县| 武城县| 聂荣县| 松江区| 南澳县| 桦甸市| 科尔| 龙江县| 长沙县| 都江堰市| 景泰县| 满洲里市| 台江县| 从化市| 西畴县| 白朗县| 博湖县| 吉水县| 贵溪市| 舟山市| 黔东| 射阳县| 云林县| 雅江县| 南丰县| 石河子市| 蒙山县| 专栏| 孝昌县| 吴川市| 彭阳县| 黄龙县| 安庆市| 任丘市| 讷河市| 寿光市| 铜梁县| 沙坪坝区| 江阴市| 常山县|