多线程执行(限制线程数)
# -*- coding: utf-8 -*-
import threading
from ctypes import *
import queue,os
import time, datetime
class store(threading.Thread):
def __init__(self, store, queue,wide,tele):
threading.Thread.__init__(self)
self.queue = queue
self.store = store
self.wide = wide
self.tele = tele
def testdll(self):
dll = CDLL(r"D:\Python\python3\Ceramic\release版\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(self.wide, 'utf-8')) # 把一组100个的字符定义为STR
tele1 = (c_char * 300)(*bytes(self.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) # 检索耗时
dic = {
"匹配系数": pchar,
"耗时": T
}
# print(dic)
return dic
def run(self):
try:
print( self.store,self.testdll())
except Exception as e:
print(e)
finally:
self.queue.get()
self.queue.task_done()
def diclist(wides,teles):
diclist = []
n=1
for i in os.listdir(wides):
widepath = os.path.join(wides, i)
for i1 in os.listdir(teles):
telepath = os.path.join(teles, i1)
dic = {
"ID":n,
"wide": widepath,
"tele": telepath
}
diclist.append(dic)
n=n+1
return diclist
def main():
q = queue.Queue(maxThreads)
for s in range(len(diclist(wides,teles))):
dic=diclist(wides, teles)[s]
q.put(s)
t = store(s, q,dic["wide"],dic["tele"])
t.start()
q.join()
if __name__ == '__main__':
wides = r"D:\Python\python3\Ceramic\release版\test"
teles = r"D:\Python\python3\Ceramic\release版\test"
maxThreads = 4
main()