예제 #1
0
    def searchAsync(self, callback, waypoints, route_params=None):
        """Search for a route given by a list of points asynchronously
        :param waypoints: 2 or more waypoints for route search
        -> the first point is the start, the last one is the destination
        -> any point in between are regarded as waypoints the route has to go through
        NOTE: not all providers might support waypoints
        :type waypoints: list
        :param route_params: further parameters for the route search
        :type route_params: RouteParameters instance
        :param controller: task controller
        :returns: a list of routes (Way objects), None if search failed
        :rtype: list
        """
        # lambda is used to pass all needed arguments to the search function
        # and passing the result to the callback,
        # but not actually executing it until the thread is started
        thread = threads.ModRanaThread(name=self._threadName)
        thread.target = lambda: self.search(
            waypoints=waypoints, route_params=route_params, controller=thread)
        thread.callback = callback

        # and yet, this really works :)
        # - we need to set the target, and it seems this can only be done in init
        # - passing the thread itself as controller seems to work or at least does
        # not raise any outright exception

        # register the thread by the thread manager
        # (this also starts the thread)
        threads.threadMgr.add(thread)
예제 #2
0
    def shutdown(self,
                 now=False,
                 join=False,
                 asynchronous=True,
                 callback=None):
        """Shutdown the lifo thread pool"""
        with self._shutdownLock:
            if self._shutdown:
                # shutdown already in progress or not running
                return False
            else:
                self._shutdown = True
        # after shutdown is set, all submit calls
        # will throw RuntimeError, so there should be
        # no danger shutdown requests will get mixed up with
        # work requests (not that it mattered BTW, as long as
        # nr shutdown requests >= nr of threads)

        # if the queue is bounded, the shutdown function may block
        # so run it in a thread by default, with the possibility of
        # triggering a callback once it is done
        if asynchronous:
            call = lambda: self._shutdownWrapper(now, join, callback)
            t = threads.ModRanaThread(name=self.name + "Shutdown", target=call)
            threads.threadMgr.add(t)
        else:
            self._shutdownWrapper(now, join, callback)

        return True
예제 #3
0
    def searchAsync(self, callback, term=None, around=None, **kwargs):
        """Perform asynchronous search
        :param callback: result handler
        :type term: a callable
        :param term: search term
        :type term: str
        :param around: optional location bias
        :type around: Point instance
        """
        # lambda is used to pass all needed arguments to the search function
        # and passing the result to the callback,
        # but not actually executing it until the thread is started
        thread = threads.ModRanaThread(name=self._threadName)
        thread.target = lambda: self.search(
            term=term, around=around, controller=thread, **kwargs)
        thread.callback = callback

        # and yet, this really works :)
        # - we need to set the target, and it seems this can only be done in init
        # - passing the thread itself as controller seems to work or at least does
        # not raise any outright exception

        # register the thread by the thread manager
        # (this also starts the thread)
        threads.threadMgr.add(thread)
예제 #4
0
파일: voice.py 프로젝트: vpodzime/modrana
 def make(self, text):
     """Queue `text` for WAV file generation."""
     if self._engine is None:
         return
     self._update_cache()
     if text in self._cache:
         # WAV file already generated, just update
         # file modification time to prevent removal.
         if self._cache[text] is not None:
             os.utime(self._cache[text])
         return
     if self._worker_thread is None:
         self._result_queue = queue.Queue()
         self._task_queue = queue.Queue()
         self._worker_thread = threads.ModRanaThread(
             name=constants.THREAD_VOICE_WORKER,
             target=lambda: voice_worker(task_queue=self._task_queue,
                                         result_queue=self._result_queue,
                                         engine=self._engine,
                                         tmpdir=self._tmpdir),
             daemon=True)
         threads.threadMgr.add(self._worker_thread)
     # Add an empty element into cache to ensure that we don't
     # run the same voice direction twice through the engine.
     self._cache[text] = None
     self._task_queue.put(text)
     self._clean_outdated_cache()
예제 #5
0
 def _start(self):
     """Start the thread pool"""
     for index in range(1, self._maxThreads + 1):
         threadName = "%sWorker%d" % (self.name, index)
         thread = threads.ModRanaThread(name=threadName,
                                        target=self._worker)
         # save the thread ID
         self._workers.append(threads.threadMgr.add(thread))
예제 #6
0
 def _start_tbt_worker(self):
     with self._tbt_worker_lock:
         # reuse previous thread or start new one
         if self._tbt_worker_enabled:
             self.log.info("reusing TBT worker thread")
         else:
             self.log.info("starting new TBT worker thread")
             t = threads.ModRanaThread(name=constants.THREAD_TBT_WORKER,
                                       target=self._tbt_worker)
             threads.threadMgr.add(t)
             self._tbt_worker_enabled = True
예제 #7
0
 def _startTileLoadingThread(self):
     """Start the sqlite loading thread"""
     t = threads.ModRanaThread(target=self._tileLoader,
                               name=constants.THREAD_TILE_STORAGE_LOADER)
     # we need that the worker tidies up,
     # (commits all "dirty" connections)
     # so it should be not daemonic
     # -> but we also cant afford that modRana wont
     # terminate completely
     # (all ModRanaThreads are daemonic by default)
     threads.threadMgr.add(t)
예제 #8
0
 def start(self, startMainLoop=False):
     """start the GPSD based location update method"""
     self.connected = False
     try:
         self.GPSDConsumer = GPSDConsumer()
         t = threads.ModRanaThread(name=constants.THREAD_GPSD_CONSUMER,
                                   target=self.GPSDConsumer._start)
         threads.threadMgr.add(t)
         self.connected = True
         self.setGPSDDebug(
             self.debug)  # check if verbose debugging is enabled
     except Exception:
         log.exception("connecting to GPSD failed")
         self.status = "No GPSD running"
예제 #9
0
파일: pools.py 프로젝트: fferner/modrana
    def startBatch(self, batch, **kwargs):
        """Process a batch"""
        with self._mutex:
            if self._running:
                log.debug("can't start another batch - already running")
            else:
                self._running = True
                self._batch = batch

                self._pool = ThreadPool(name=self.name,
                                        maxThreads=self._maxThreads())

                # start the loading thread
                t = threads.ModRanaThread(name=self.name+"Loader", target=self._loadItems)
                self._loaderName = threads.threadMgr.add(t)
예제 #10
0
 def _startTileLoadingManager(self):
     """Start the consumer thread for download requests"""
     t = threads.ModRanaThread(name=constants.THREAD_TILE_DOWNLOAD_MANAGER,
                               target = self._tileLoadingManager)
     threads.threadMgr.add(t)
예제 #11
0
                # shutdown already in progress or not running
                return False
            else:
                self._shutdown = True
        # after shutdown is set, all submit calls
        # will throw RuntimeError, so there should be
        # no danger shutdown requests will get mixed up with
        # work requests (not that it mattered BTW, as long as
        # nr shutdown requests >= nr of threads)

        # if the queue is bounded, the shutdown function may block
        # so run it in a thread by default, with the possibility of
        # triggering a callback once it is done
        if async:
            call = lambda : self._shutdownWrapper(now, join, callback)
            t = threads.ModRanaThread(name=self.name+"Shutdown",
                                      target=call)
            threads.threadMgr.add(t)
        else:
            self._shutdownWrapper(now, join, callback)

        return True


    def _shutdownWrapper(self, now, join, callback):
        self._shutdownHandler(now, join)
        # is there a callback for when we are done ?
        if callback:
            # call it !
            callback()

    def _shutdownHandler(self, now, join):