hydrus/include/HydrusThreading.py

245 lines
6.0 KiB
Python
Raw Normal View History

2015-04-29 19:20:35 +00:00
import HydrusExceptions
2014-05-21 21:37:35 +00:00
import Queue
import random
import threading
import time
import traceback
import wx
2015-03-25 22:04:19 +00:00
import HydrusData
import HydrusGlobals
2014-05-21 21:37:35 +00:00
class DAEMON( threading.Thread ):
def __init__( self, name, period = 1200 ):
threading.Thread.__init__( self, name = name )
self._name = name
self._event = threading.Event()
2015-03-25 22:04:19 +00:00
HydrusGlobals.pubsub.sub( self, 'shutdown', 'shutdown' )
2014-05-21 21:37:35 +00:00
def shutdown( self ): self._event.set()
class DAEMONQueue( DAEMON ):
def __init__( self, name, callable, queue_topic, period = 10 ):
DAEMON.__init__( self, name )
self._callable = callable
self._queue = Queue.Queue()
self._queue_topic = queue_topic
self._period = period
2015-03-25 22:04:19 +00:00
HydrusGlobals.pubsub.sub( self, 'put', queue_topic )
2014-05-21 21:37:35 +00:00
self.start()
def put( self, data ): self._queue.put( data )
def run( self ):
time.sleep( 3 )
while True:
while self._queue.empty():
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2014-05-21 21:37:35 +00:00
self._event.wait( self._period )
self._event.clear()
2015-03-25 22:04:19 +00:00
while wx.GetApp().JustWokeFromSleep():
2014-10-29 21:39:01 +00:00
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2014-10-29 21:39:01 +00:00
time.sleep( 10 )
2014-05-21 21:37:35 +00:00
items = []
while not self._queue.empty(): items.append( self._queue.get() )
2015-03-04 22:44:32 +00:00
try:
self._callable( items )
2015-04-29 19:20:35 +00:00
except HydrusExceptions.ShutdownException:
return
2015-03-04 22:44:32 +00:00
except Exception as e:
2015-03-25 22:04:19 +00:00
HydrusData.ShowException( e )
2015-03-04 22:44:32 +00:00
2014-05-21 21:37:35 +00:00
class DAEMONWorker( DAEMON ):
2015-03-25 22:04:19 +00:00
def __init__( self, name, callable, topics = None, period = 1200, init_wait = 3, pre_callable_wait = 3 ):
if topics is None: topics = []
2014-05-21 21:37:35 +00:00
DAEMON.__init__( self, name )
self._callable = callable
self._topics = topics
self._period = period
self._init_wait = init_wait
2014-07-23 21:21:37 +00:00
self._pre_callable_wait = pre_callable_wait
2014-05-21 21:37:35 +00:00
2015-03-25 22:04:19 +00:00
for topic in topics: HydrusGlobals.pubsub.sub( self, 'set', topic )
2014-05-21 21:37:35 +00:00
self.start()
def run( self ):
self._event.wait( self._init_wait )
while True:
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2014-05-21 21:37:35 +00:00
2014-07-23 21:21:37 +00:00
time.sleep( self._pre_callable_wait )
2015-06-17 20:01:41 +00:00
if HydrusGlobals.shutdown: return
2015-03-25 22:04:19 +00:00
while wx.GetApp().JustWokeFromSleep():
2014-10-29 21:39:01 +00:00
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2014-10-29 21:39:01 +00:00
time.sleep( 10 )
2015-04-29 19:20:35 +00:00
try:
self._callable()
except HydrusExceptions.ShutdownException:
return
2014-07-23 21:21:37 +00:00
except Exception as e:
2015-03-25 22:04:19 +00:00
HydrusData.ShowText( 'Daemon ' + self._name + ' encountered an exception:' )
2014-09-24 21:50:07 +00:00
2015-03-25 22:04:19 +00:00
HydrusData.ShowException( e )
2014-07-23 21:21:37 +00:00
2014-05-21 21:37:35 +00:00
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2014-05-21 21:37:35 +00:00
self._event.wait( self._period )
self._event.clear()
def set( self, *args, **kwargs ): self._event.set()
class DAEMONCallToThread( DAEMON ):
def __init__( self ):
DAEMON.__init__( self, 'CallToThread' )
self._queue = Queue.Queue()
self.start()
def put( self, callable, *args, **kwargs ):
self._queue.put( ( callable, args, kwargs ) )
self._event.set()
def run( self ):
while True:
while self._queue.empty():
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2014-05-21 21:37:35 +00:00
self._event.wait( 1200 )
self._event.clear()
2014-05-28 21:03:24 +00:00
try:
( callable, args, kwargs ) = self._queue.get()
callable( *args, **kwargs )
2014-06-18 21:53:48 +00:00
del callable
2015-04-29 19:20:35 +00:00
except HydrusExceptions.ShutdownException:
return
2015-03-04 22:44:32 +00:00
except Exception as e:
2015-03-25 22:04:19 +00:00
HydrusData.ShowException( e )
2015-03-04 22:44:32 +00:00
2014-05-21 21:37:35 +00:00
2014-05-28 21:03:24 +00:00
time.sleep( 0.00001 )
2014-05-21 21:37:35 +00:00
call_to_threads = [ DAEMONCallToThread() for i in range( 10 ) ]
def CallToThread( callable, *args, **kwargs ):
call_to_thread = random.choice( call_to_threads )
while call_to_thread == threading.current_thread: call_to_thread = random.choice( call_to_threads )
call_to_thread.put( callable, *args, **kwargs )
2015-03-18 21:46:29 +00:00
def CallBlockingToWx( callable, *args, **kwargs ):
def wx_code( job_key ):
try:
result = callable( *args, **kwargs )
job_key.SetVariable( 'result', result )
except Exception as e:
2015-04-08 18:10:50 +00:00
print( 'CallBlockingToWx just caught this error:' )
print( traceback.format_exc() )
2015-03-18 21:46:29 +00:00
job_key.SetVariable( 'error', e )
finally: job_key.Finish()
2015-03-25 22:04:19 +00:00
job_key = HydrusData.JobKey()
2015-03-18 21:46:29 +00:00
job_key.Begin()
wx.CallAfter( wx_code, job_key )
while not job_key.IsDone():
2015-03-25 22:04:19 +00:00
if HydrusGlobals.shutdown: return
2015-03-18 21:46:29 +00:00
time.sleep( 0.05 )
if job_key.HasVariable( 'result' ): return job_key.GetVariable( 'result' )
else: raise job_key.GetVariable( 'error' )
2014-05-21 21:37:35 +00:00