多进程执行(限制进程数)

多进程执行(限制进程数)

from ctypes import *
import os
import multiprocessing
import time, datetime
import multiprocessing as np

def testdll(wide,tele):
    dll = CDLL(os.getcwd()+"/"+"SealDet_surf_ncc_V22_vs2017_akaze_release_dll.dll")#初始化dll,加载dll
    dll.SealDet_surf_ncc.argtypes = [POINTER(c_char), POINTER(c_char), c_int, c_float] #定义dll入参类型
    dll.SealDet_surf_ncc.restype = c_float   #定义dll出参类型,不定义程序不知道类型会报错
    wide1 = (c_char * 300)(*bytes(wide, 'utf-8'))  # 把一组100个的字符定义为STR
    tele1 = (c_char * 300)(*bytes(tele, 'utf-8'))  # 把一组100个的字符定义为STR
    #cast(wide1, POINTER(c_char))
    #cast(tele1, POINTER(c_char))
    T1 = datetime.datetime.now()
    pchar = dll.SealDet_surf_ncc(wide1, tele1, 20, 0.1)
    T2 = datetime.datetime.now()
    T = round((T2 - T1).total_seconds(), 3)  # 检索耗时
    return {
        "匹配系数":pchar,
        "耗时":T
    }

def diclist(wide,tele):
    diclist = []
    n=1
    for i in os.listdir(wide):
        widepath = os.path.join(wide, i)
        for i1 in os.listdir(tele):
            telepath = os.path.join(tele, i1)
            dic = {
                "ID":n,
                "wide": widepath,
                "tele": telepath
            }
            diclist.append(dic)
            n=n+1
    return diclist

def test_xxx(wide,tele,c):
    print(c,testdll(wide,tele))

def main2():
    pool = multiprocessing.Pool(processes=4)
    for i in range(len(diclist(wide,tele))):
        path = diclist(wide, tele)[i]
        pool.apply_async(func=test_xxx, args=(path["wide"],path["tele"],i+1))
    pool.close()
    pool.join()  # 在join之前一定要调用close,否则报错

if __name__ == "__main__":
    wide = r"D:\Python\python3\Ceramic\release版\test"
    tele = r"D:\Python\python3\Ceramic\release版\test"


    T1 = datetime.datetime.now()
    print(T1)
    main2()
    T2 = datetime.datetime.now()
    print(T2)
    print(round((T2 - T1).total_seconds(), 3))


你可能感兴趣的:(python3)