Esempio n. 1
0
    def __init__(self, pool_size):
        # Only allow creation via get_instance
        if not type(self)._SINGLETON_LOCK._is_owned():
            raise AsyncArcticException(
                "{} is a singleton, can't create a new instance".format(
                    type(self)))

        pool_size = int(pool_size)
        if pool_size < 1:
            raise ValueError(
                "{} can't be instantiated with a pool_size of {}".format(
                    type(self), pool_size))

        # Enforce the singleton pattern
        with type(self)._SINGLETON_LOCK:
            if type(self)._instance is not None:
                raise AsyncArcticException(
                    "LazySingletonTasksCoordinator is a singleton, can't create a new instance"
                )
            self._lock = RLock()
            self._pool = None
            self._pool_size = int(pool_size)
            self._pool_update_hooks = []
            self.alive_tasks = {}
            self.is_shutdown = False
Esempio n. 2
0
 def await_termination(self, timeout=None):
     with type(self)._POOL_LOCK:
         if not self.is_shutdown:
             raise AsyncArcticException("The workers pool has not been shutdown, please call shutdown() first.")
     LazySingletonTasksCoordinator.wait_tasks(
         [v[0] for v in itervalues(self.alive_tasks)],
         timeout=timeout, return_when=ALL_COMPLETED, raise_exceptions=False)
     with type(self)._POOL_LOCK:
         self.alive_tasks = {}
Esempio n. 3
0
    def submit_task(self, is_looping, fun, *args, **kwargs):
        new_id = uuid.uuid4()
        shutdown_flag = Event() if is_looping else None
        with type(self)._POOL_LOCK:
            if self.is_shutdown:
                raise AsyncArcticException("The worker pool has been shutdown and can no longer accept new requests.")

            if is_looping:
                new_future = self._workers_pool.submit(_looping_task, shutdown_flag, fun, *args, **kwargs)
            else:
                new_future = self._workers_pool.submit(_exec_task, fun, *args, **kwargs)
            self.alive_tasks = {k: v for k, v in iteritems(self.alive_tasks) if not v[0].done()}
            self.alive_tasks[new_id] = (new_future, shutdown_flag)
        return new_id, new_future