最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用線程池傳遞多個(gè)參數(shù)的幾種方法

 更新時(shí)間:2026年04月14日 09:34:14   作者:獨(dú)隅  
本文介紹了在Python中使用線程池傳遞多個(gè)參數(shù)的四種方法,并推薦使用stmap方式,這種方法代碼清晰易讀,參數(shù)傳遞靈活,易于維護(hù)和擴(kuò)展,適用于為每個(gè)設(shè)備指定不同參數(shù)的場(chǎng)景,需要的朋友可以參考下

在 Python 中使用線程池傳遞多個(gè)參數(shù)有多種方法,以下是幾種常用的方式:

方法一:使用zip和*解包

from concurrent.futures import ThreadPoolExecutor

def worker(phone, x, y, duration):
    print(f"設(shè)備: {phone}, 位置: ({x}, {y}), 時(shí)長(zhǎng): {duration}")
    # 這里執(zhí)行實(shí)際的投屏操作

def thread_pool_with_zip():
    phones = ["172.26.101.164", "172.26.101.112", "172.26.101.113"]
    x_positions = [100, 500, 900]  # X坐標(biāo)
    y_positions = [100, 100, 100]  # Y坐標(biāo)
    durations = [20, 25, 30]  # 投屏?xí)r長(zhǎng)
    
    with ThreadPoolExecutor(max_workers=3) as executor:
        # 使用zip打包參數(shù),然后解包傳遞給worker函數(shù)
        executor.map(worker, phones, x_positions, y_positions, durations)

方法二:使用partial函數(shù)固定部分參數(shù)

from concurrent.futures import ThreadPoolExecutor
from functools import partial

def worker(phone, x, y, duration):
    print(f"設(shè)備: {phone}, 位置: ({x}, {y}), 時(shí)長(zhǎng): {duration}")

def thread_pool_with_partial():
    phones = ["172.26.101.164", "172.26.101.112", "172.26.101.113"]
    
    # 固定x, y, duration參數(shù)
    fixed_worker = partial(worker, x=100, y=100, duration=20)
    
    with ThreadPoolExecutor(max_workers=3) as executor:
        executor.map(fixed_worker, phones)

方法三:使用字典或元組包裝參數(shù)

from concurrent.futures import ThreadPoolExecutor

def worker(params):
    phone = params['phone']
    x = params['x']
    y = params['y']
    duration = params['duration']
    print(f"設(shè)備: {phone}, 位置: ({x}, {y}), 時(shí)長(zhǎng): {duration}")

def thread_pool_with_dict():
    # 參數(shù)列表
    params_list = [
        {'phone': "172.26.101.164", 'x': 100, 'y': 100, 'duration': 20},
        {'phone': "172.26.101.112", 'x': 500, 'y': 100, 'duration': 25},
        {'phone': "172.26.101.113", 'x': 900, 'y': 100, 'duration': 30}
    ]
    
    with ThreadPoolExecutor(max_workers=3) as executor:
        executor.map(worker, params_list)

方法四:使用starmap(推薦)

from concurrent.futures import ThreadPoolExecutor

def worker(phone, x, y, duration):
    print(f"設(shè)備: {phone}, 位置: ({x}, {y}), 時(shí)長(zhǎng): {duration}")

def thread_pool_with_starmap():
    # 參數(shù)元組列表
    args_list = [
        ("172.26.101.164", 100, 100, 20),
        ("172.26.101.112", 500, 100, 25),
        ("172.26.101.113", 900, 100, 30)
    ]
    
    with ThreadPoolExecutor(max_workers=3) as executor:
        # 使用starmap解包元組參數(shù)
        executor.map(lambda args: worker(*args), args_list)

在您的代碼中應(yīng)用

修改您的代碼以支持多參數(shù)傳遞:

def Scrcpy(params):
    """接收參數(shù)字典的投屏函數(shù)"""
    phone = params['phone']
    x = params.get('x', 100)  # 默認(rèn)值
    y = params.get('y', 100)  # 默認(rèn)值
    duration = params.get('duration', 20)  # 默認(rèn)值
    
    RestartScrcpy(phone)
    time.sleep(5)
    phone_num = Phone_number(phone)
    
    try:
        _IP_CONNEXT = f"{phone}:12324"
        run_cmd(f"adb connect {_IP_CONNEXT}")  
        # 添加窗口位置參數(shù)
        run_cmd(f'scrcpy -s {_IP_CONNEXT} --time-limit {duration} --max-size=1280 --window-title={phone_num} --window-x={x} --window-y={y}') 
        run_cmd(f"adb disconnect {_IP_CONNEXT}")
    except Exception as e:
        log(logging.ERROR, f"Scrcpy: {e}")

def thread_pool_multiple_params():
    # 定義設(shè)備及其參數(shù)
    device_configs = [
        {'phone': "172.26.101.164", 'x': 100, 'y': 100, 'duration': 20},
        {'phone': "172.26.101.112", 'x': 800, 'y': 100, 'duration': 25},
        # 添加更多設(shè)備...
    ]
    
    try:
        with ThreadPoolExecutor(max_workers=len(device_configs)) as executor:
            executor.map(Scrcpy, device_configs)
    except Exception as e:
        log(logging.ERROR, f"線程池異常: {e}")

# 在主函數(shù)中使用
if __name__ == '__main__':
    while True:
        if len(PHONES) >= 2:
            selected = random.sample(PHONES, 2)
            log(logging.DEBUG, f"--> 隨機(jī)選取的投屏設(shè)備:{selected}")
            
            # 為每個(gè)設(shè)備創(chuàng)建參數(shù)配置
            configs = []
            for i, phone in enumerate(selected):
                configs.append({
                    'phone': phone,
                    'x': 100 + i * 700,  # 水平排列窗口
                    'y': 100,
                    'duration': 20
                })
            
            thread_pool_multiple_params()
            time.sleep(5)
        # ... 其他條件判斷

使用進(jìn)程池(multiprocessing.Pool)的多參數(shù)傳遞

import multiprocessing

def worker(args):
    phone, x, y, duration = args
    print(f"設(shè)備: {phone}, 位置: ({x}, {y}), 時(shí)長(zhǎng): {duration}")

def multiprocessing_pool_multiple_params():
    args_list = [
        ("172.26.101.164", 100, 100, 20),
        ("172.26.101.112", 500, 100, 25),
        ("172.26.101.113", 900, 100, 30)
    ]
    
    with multiprocessing.Pool(processes=3) as pool:
        pool.map(worker, args_list)

推薦方案

對(duì)于您的場(chǎng)景,我推薦使用方法四(starmap方式),因?yàn)樗?/p>

  1. 代碼清晰易讀
  2. 參數(shù)傳遞靈活
  3. 易于維護(hù)和擴(kuò)展

這樣您就可以為每個(gè)設(shè)備指定不同的窗口位置、投屏?xí)r長(zhǎng)等參數(shù)了。

到此這篇關(guān)于Python使用線程池傳遞多個(gè)參數(shù)的幾種方法的文章就介紹到這了,更多相關(guān)Python線程池傳遞多個(gè)參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 只用40行Python代碼就能寫(xiě)出pdf轉(zhuǎn)word小工具

    只用40行Python代碼就能寫(xiě)出pdf轉(zhuǎn)word小工具

    今天咱們介紹一個(gè)pdf轉(zhuǎn)word的免費(fèi)小工具,滿足這么一個(gè)不常見(jiàn)但是偶爾會(huì)出來(lái)煩人的需求文中有非常詳細(xì)的代碼示例,對(duì)小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Python實(shí)現(xiàn)讀取文本文件并轉(zhuǎn)換為pdf

    Python實(shí)現(xiàn)讀取文本文件并轉(zhuǎn)換為pdf

    這篇文章主要為大家詳細(xì)介紹了如何使用Python簡(jiǎn)便快捷地完成TXT文件到PDF文檔的轉(zhuǎn)換,滿足多樣化的文檔處理需求,感興趣的小伙伴可以參考下
    2024-04-04
  • Expected conditions模塊使用方法匯總代碼解析

    Expected conditions模塊使用方法匯總代碼解析

    這篇文章主要介紹了Expected conditions模塊使用方法匯總代碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python使用pycharm環(huán)境調(diào)用opencv庫(kù)

    python使用pycharm環(huán)境調(diào)用opencv庫(kù)

    這篇文章主要介紹了python使用pycharm環(huán)境調(diào)用opencv庫(kù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • opencv python在視屏上截圖功能的實(shí)現(xiàn)

    opencv python在視屏上截圖功能的實(shí)現(xiàn)

    OpenCV是一個(gè)基于BSD許可(開(kāi)源)發(fā)行的跨平臺(tái)計(jì)算機(jī)視覺(jué)庫(kù),可以運(yùn)行在Linux、Windows、Android和Mac OS操作系統(tǒng)上。這篇文章主要介紹了opencv python在視屏上截圖,需要的朋友可以參考下
    2020-03-03
  • 關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別

    關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別

    這篇文章主要介紹了關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別,queue隊(duì)列中的put()或者get()方法中都提供了timeout參數(shù),利用這個(gè)參數(shù)可以有效解決上述消除不能消費(fèi)和線程一直阻塞問(wèn)題,需要的朋友可以參考下
    2023-05-05
  • Python實(shí)現(xiàn)HTML文件或字符串轉(zhuǎn)換為純文本TXT

    Python實(shí)現(xiàn)HTML文件或字符串轉(zhuǎn)換為純文本TXT

    在數(shù)據(jù)處理、內(nèi)容提取、網(wǎng)頁(yè)歸檔等任務(wù)中,經(jīng)常需要將 HTML 轉(zhuǎn)換為純文本 TXT,本文將介紹如何用 Python 和 Free Spire.Doc 庫(kù)完成 HTML 到 TXT 的轉(zhuǎn)換,希望對(duì)大家有所幫助
    2025-09-09
  • Python實(shí)現(xiàn)簡(jiǎn)單線性插值去馬賽克算法代碼示例

    Python實(shí)現(xiàn)簡(jiǎn)單線性插值去馬賽克算法代碼示例

    去馬賽克是圖像處理中的一項(xiàng)技術(shù),用于從單色彩濾光片陣列(CFA)圖像恢復(fù)全彩圖像,本文介紹了一種基于簡(jiǎn)單線性插值的去馬賽克算法,并展示了如何將MATLAB代碼轉(zhuǎn)換為Python代碼,需要的朋友可以參考下
    2024-10-10
  • Django 用戶認(rèn)證組件使用詳解

    Django 用戶認(rèn)證組件使用詳解

    這篇文章主要介紹了Django 用戶認(rèn)證組件使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python使用大語(yǔ)言模型進(jìn)行圖表可視化

    Python使用大語(yǔ)言模型進(jìn)行圖表可視化

    Python使用matplotlib進(jìn)行可視化一直有2個(gè)問(wèn)題,一是代碼繁瑣,二是默認(rèn)模板比較丑,在大模型時(shí)代,這個(gè)問(wèn)題有了另一種解法,下面我們就來(lái)看看如何使用大語(yǔ)言模型進(jìn)行圖表可視化吧
    2025-04-04

最新評(píng)論

巨鹿县| 新晃| 会宁县| 郁南县| 哈尔滨市| 昆山市| 白银市| 龙南县| 海伦市| 宜阳县| 华安县| 恩平市| 虞城县| 克东县| 宜宾市| 镇赉县| 水城县| 永定县| 大丰市| 武鸣县| 荣昌县| 额济纳旗| 中西区| 文成县| 林口县| 云梦县| 通海县| 满洲里市| 年辖:市辖区| 新疆| 阿荣旗| 合山市| 海丰县| 连云港市| 正定县| 昌图县| 高阳县| 象州县| 通州区| 金阳县| 宁德市|