1. 程式人生 > 其它 >python 多程序執行緒之傳入多引數問題

python 多程序執行緒之傳入多引數問題

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

# -------------------------------------------------------------------------------
# Name:         tools
# Author:       yunhgu
# Date:         2021/7/1 11:06
# Description: 
# -------------------------------------------------------------------------------
from concurrent.futures import ProcessPoolExecutor, as_completed
from multiprocessing import Pool
import time
import os


def work(a):
    time.sleep(1)
    print(f"{a}:{os.getpid()}")
    return a


def main():
    with ProcessPoolExecutor(max_workers=None) as p:
        task_list = []
        for i in range(3):
            for j in range(3):
                for z in range(3):
                    a = (i, j, z)
                    task = p.submit(work, a)
                    task_list.append(task)
        for task in as_completed(task_list):
            if task.done():
                print(task.result())


if __name__ == '__main__':
    start_time = time.time()
    main()
    print(time.time() - start_time)
    # main2()