hydrus/include/HydrusThreading.py

182 lines
4.4 KiB
Python
Raw Normal View History

2014-05-21 21:37:35 +00:00
import collections
import HydrusConstants as HC
import itertools
import os
import Queue
import random
import threading
import time
import traceback
import wx
class DAEMON( threading.Thread ):
def __init__( self, name, period = 1200 ):
threading.Thread.__init__( self, name = name )
self._name = name
self._event = threading.Event()
HC.pubsub.sub( self, 'shutdown', 'shutdown' )
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
HC.pubsub.sub( self, 'put', queue_topic )
self.start()
def put( self, data ): self._queue.put( data )
def run( self ):
time.sleep( 3 )
while True:
while self._queue.empty():
if HC.shutdown: return
self._event.wait( self._period )
self._event.clear()
2014-10-29 21:39:01 +00:00
while HC.app.JustWokeFromSleep():
if HC.shutdown: return
time.sleep( 10 )
2014-05-21 21:37:35 +00:00
items = []
while not self._queue.empty(): items.append( self._queue.get() )
try: self._callable( items )
except Exception as e: HC.ShowException( e )
class DAEMONWorker( DAEMON ):
2014-07-30 21:18:17 +00:00
def __init__( self, name, callable, topics = [], period = 1200, init_wait = 3, pre_callable_wait = 3 ):
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
for topic in topics: HC.pubsub.sub( self, 'set', topic )
self.start()
def run( self ):
self._event.wait( self._init_wait )
while True:
if HC.shutdown: return
2014-07-23 21:21:37 +00:00
time.sleep( self._pre_callable_wait )
2014-10-29 21:39:01 +00:00
while HC.app.JustWokeFromSleep():
if HC.shutdown: return
time.sleep( 10 )
2014-05-21 21:37:35 +00:00
try: self._callable()
2014-07-23 21:21:37 +00:00
except Exception as e:
2014-08-06 20:29:17 +00:00
HC.ShowText( 'Daemon ' + self._name + ' encountered an exception:' )
2014-09-24 21:50:07 +00:00
2014-07-23 21:21:37 +00:00
HC.ShowException( e )
2014-05-21 21:37:35 +00:00
if HC.shutdown: return
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():
if HC.shutdown: return
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
2014-05-21 21:37:35 +00:00
except Exception as e: HC.ShowException( e )
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 )