多執行緒執行(限制執行緒數)
# -*- 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
deftestdll
(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 =
# print(dic)
return dic
defrun(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=1for i in os.listdir(wides)
: widepath = os.path.join(wides, i)
for i1 in os.listdir(teles)
: telepath = os.path.join(teles, i1)
dic =
n=n+
1return diclist
defmain()
: 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(
)
c semaphoreSlim限制執行緒數
我們在使用執行緒的時候,如果多個執行緒數去訪問乙個資源的時候,那麼是非常影響程式的執行的,因為如果有寫的操作,那麼需要寫鎖,那麼執行緒都會堵在同乙個地方,那麼我們可以限制一下訪問乙個資源的執行緒數。static semaphoreslim semaphore new semaphoreslim 4 ...
C 多執行緒 限制執行緒數量
這裡需要用到互斥鎖mutex,條件變數condition variable,通用互斥鎖包裝器unique lock std unique lock也可以提供自動加鎖 解鎖功能,比std lock guard更加靈活。類 unique lock 是通用互斥包裝器,允許延遲鎖定 鎖定的有時限嘗試 遞迴鎖...
python多執行緒限制併發數示例
coding utf 8 usr bin env python import queue import threading import time prolock threading.lock 定義同時佇列數 queue queue.queue maxsize 10 定義任務初值值及最大值 task...