def __init__(self, period=RPCPERIOD, timeout=TIMEOUT, maxConcurrentRequests=MAXCONCURRENT): self.queue = deque() self.thread = PeriodicThread(self._sendRequest, period=period) self.queueLock = TimeoutLock() self.threadLock = TimeoutLock() self.activeRequests = [] self.socketPool = GoogleSocketPool(maxConcurrentRequests, timeout)
def cacheThread(func): def checkCacheSize(): print 'checking cache size' s = os.statvfs('/tmp') freeSpace = (s.f_bavail * s.f_frsize) / 1024 if freeSpace < 5000000: evictCache() # signal.signal(signal.SIGINT, t.cancel()) t = PeriodicThread(callback=checkCacheSize,period=30) t.start() return func
def __zoom_in(self): self._camera.zoom_in() def __zoom_out(self): self._camera.zoom_out() def __delete_cur_session(self): self._periodic_thread.cancel() shutil.rmtree(self._session_manager.current_session.session_path) self._session_manager.reset_cur_session() self._periodic_thread.start() def __show_frame(self, image_name): return self._camera.show_frame(image_name) def __show_next_frame(self): img_iter = self._session_manager.current_session.get_img_iterator() if img_iter: img = next(img_iter) if img: self.__show_frame(img) else: self.__show_frame('res/logo.jpg') if __name__ == '__main__': spasso1 = SpassoUno(SessionManager(), Camera(), PeriodicThread(period=1.5)) spasso1.run() spasso1.cleanup()
class RPCDispatcher(object): # Responsible for queueing up requests and passing the response back correctly # Mainly it is there so as to not overflow the Google capacity for our service (I don't want to have pay) def __init__(self, period=RPCPERIOD, timeout=TIMEOUT, maxConcurrentRequests=MAXCONCURRENT): self.queue = deque() self.thread = PeriodicThread(self._sendRequest, period=period) self.queueLock = TimeoutLock() self.threadLock = TimeoutLock() self.activeRequests = [] self.socketPool = GoogleSocketPool(maxConcurrentRequests, timeout) @contextmanager def createGoogleRequest(self): with self.socketPool.getSocket() as http: yield discovery.build('speech', 'v1beta1', http=http, discoveryServiceUrl=DISCOVERY_URL) def start(self): self.thread.start() def stop(self): self.thread.cancel() with self.threadLock: activeRequests = self.activeRequests for request in activeRequests: request.join() def queueRequest(self, request): future = Future() rospy.logdebug("Getting queue lock in queue request") with self.queueLock: rospy.logdebug("Got queue lock in queue request") self.queue.appendleft((request, future)) return future def _sendRequest(self): rospy.logdebug("Getting queue lock in send request") with self.queueLock: rospy.logdebug("Got queue lock in send request") try: request, future = self.queue.pop() except IndexError: rospy.logdebug("No pending requests") return rospy.logdebug("Sending next request") # Google does not support asynchronous requests so transform it into one that does, also fetch token thread = threading.Thread(target=self.__sendRequest, args=(request, future)) rospy.logdebug("Getting thread lock on send request") with self.threadLock: rospy.logdebug("Got thread lock on send request") self.activeRequests.append(thread) thread.start() def __sendRequest(self, body, future): if not USE_TRANSCRIPTION: future.set_exception( RequestMethodError("Transcription is disabled")) thread = threading.currentThread() try: with self.createGoogleRequest() as request: service = request.speech().syncrecognize(body=body) rospy.logdebug("Sending request for thread {}".format( thread.name)) response = service.execute() except Exception as e: rospy.logdebug( "Getting thread lock on send request within thread {}".format( thread.name)) with self.threadLock: rospy.logdebug( "Got thread lock on send request within thread {}".format( thread.name)) self.activeRequests.remove(thread) future.set_exception(e) return rospy.logdebug("Received response {} for thread {}".format( response, thread.name)) future.set_result(response) rospy.logdebug( "Getting thread lock on send request within thread {}".format( thread.name)) with self.threadLock: rospy.logdebug( "Got thread lock on send request within thread {}".format( thread.name)) self.activeRequests.remove(thread)
from MyAlgorithm import MyAlgorithm from periodic_thread import PeriodicThread import sys USE_GUI = True if "--no-gui" in sys.argv: USE_GUI=False if USE_GUI: from gui import minimal_gui, qtimshow from PyQt4 import QtGui import sys if __name__ == '__main__': sensor = Sensor() t1 = PeriodicThread(0.025, sensor.update) t1.setDaemon(True) t1.start() algorithm = MyAlgorithm(sensor) t2 = PeriodicThread(0.033, algorithm.execute) t2.setDaemon(True) t2.start() if USE_GUI: app = QtGui.QApplication(sys.argv) #imageDisplay = minimal_gui.ImgDisplay() qtimshow.enable() #algorithm.debugImg = lambda(img): imageDisplay.Q_SIGNAL_ImageUpdate.emit(img) #qtimshow.disable()