也許你可以調用該函數的線程異步執行,將它們添加到你自己的隊列或設置一個條件,如果它已經在運行,則不執行,這將停止填充那個失敗的消息泵。
選項1.這將函數執行添加到隊列線程:
import pythoncom, pyHook, threading
lock = threading.Lock()
def myFunc(i):
lock.acquire() #execute next function until previous has finished
#some code
lock.release()
def OnKeyboardEvent(event):
keyPressed = chr(event.Ascii)
if keyPressed == 'z':
t = threading.Thread(target=myFunc, args=(1,)) #added to queue
t.start()
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
選項2或者這會忽略其他處理呼叫,如果它是忙:
def myFunc(i):
myFunc.isRunning = True
#some code
myFunc.isRunning = False
myFunc.isRunning = False
def OnKeyboardEvent(event):
keyPressed = chr(event.Ascii)
if keyPressed == 'z':
if not myFunc.isRunning: #if function is being executed ignore this call
t = threading.Thread(target=myFunc,args=(1,))
t.start()
return True
當然
當您通過捕獲異常來添加代碼時,您應該小心,否則線程將保持阻塞狀態。
我有一個很熟悉的問題! 2010?我完蛋了。 – 2012-03-19 04:38:32