context.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:pyactor 作者: pedrotgn 项目源码 文件源码
def spawn(self, aid, klass, *param, **kparam):
        '''
        This method creates an actor attached to this host. It will be
        an instance of the class *klass* and it will be assigned an ID
        that identifies it among the host.

        This method can be called remotely synchronously.

        :param str. aid: identifier for the spawning actor. Unique within
            the host.
        :param class klass: class type of the spawning actor. If you are
            spawning remotely and the class is not in the server module,
            you must specify here the path to that class in the form
            'module.py/Class' so the server can import the class and create
            the instance.
        :param param-kparam: arguments for the init function of the
            spawning actor class.
        :return: :class:`~.Proxy` of the actor spawned.
        :raises: :class:`AlreadyExistsError`, if the ID specified is
            already in use.
        :raises: :class:`HostDownError` if the host is not initiated.
        '''
        if param is None:
            param = []
        if not self.alive:
            raise HostDownError()
        if isinstance(klass, basestring):
            module, klass = klass.split('/')
            module_ = __import__(module, globals(), locals(),
                                 [klass], -1)
            klass_ = getattr(module_, klass)
        elif isinstance(klass, (types.TypeType, types.ClassType)):
            klass_ = klass
        url = '%s://%s/%s' % (self.transport, self.host_url.netloc, aid)
        if url in self.actors.keys():
            raise AlreadyExistsError(url)
        else:
            obj = klass_(*param, **kparam)
            obj.id = aid
            obj.url = url
            if self.running:
                obj.host = self.proxy
            # else:
            #     obj.host = Exception("Host is not an active actor. \
            #                           Use 'init_host' to make it alive.")

            if hasattr(klass_, '_parallel') and klass_._parallel:
                new_actor = parallels.ActorParallel(url, klass_, obj)
                lock = new_actor.get_lock()
                self.locks[url] = lock
            else:
                new_actor = actor.Actor(url, klass_, obj)

            obj.proxy = Proxy(new_actor)
            self.launch_actor(url, new_actor)
            return Proxy(new_actor)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号