Add threading utilities
This commit is contained in:
parent
9c32dfbb38
commit
e850402095
19
threadutils.py
Normal file
19
threadutils.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
import Queue
|
||||
import threading
|
||||
|
||||
# This queue is for other threads that want to execute code in the main thread
|
||||
eventQueue = Queue.Queue()
|
||||
|
||||
def runInThread(threadFunc, callback):
|
||||
"""
|
||||
Executes threadFunc in a new thread. The result of threadFunc will be
|
||||
pass as the first argument to callback. callback will be called in the main
|
||||
thread.
|
||||
"""
|
||||
def helper():
|
||||
# Execute threadfunc in new thread
|
||||
result = threadFunc()
|
||||
# Queue callback to be call in main thread
|
||||
eventQueue.put(lambda: callback(result))
|
||||
thread = threading.Thread(target=helper)
|
||||
thread.start()
|
10
transwhat.py
10
transwhat.py
|
@ -29,8 +29,8 @@ import logging
|
|||
import asyncore
|
||||
import sys, os
|
||||
import e4u
|
||||
import threading
|
||||
import Queue
|
||||
import threadutils
|
||||
|
||||
sys.path.insert(0, os.getcwd())
|
||||
|
||||
|
@ -89,6 +89,7 @@ plugin.handleBackendConfig({
|
|||
],
|
||||
})
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
asyncore.loop(timeout=1.0, count=10, use_poll = True)
|
||||
|
@ -101,6 +102,13 @@ while True:
|
|||
break
|
||||
if closed:
|
||||
break
|
||||
while True:
|
||||
try:
|
||||
callback = threadutils.eventQueue.get_nowait()
|
||||
except Queue.Empty:
|
||||
break
|
||||
else:
|
||||
callback()
|
||||
except SystemExit:
|
||||
break
|
||||
except:
|
||||
|
|
Loading…
Reference in a new issue