当前位置: 首页>>代码示例>>Python>>正文


Python SocketManager.bind方法代码示例

本文整理汇总了Python中agentzero.core.SocketManager.bind方法的典型用法代码示例。如果您正苦于以下问题:Python SocketManager.bind方法的具体用法?Python SocketManager.bind怎么用?Python SocketManager.bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在agentzero.core.SocketManager的用法示例。


在下文中一共展示了SocketManager.bind方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_socket_bind

# 需要导入模块: from agentzero.core import SocketManager [as 别名]
# 或者: from agentzero.core.SocketManager import bind [as 别名]
def test_socket_bind(get_by_name, register_socket, engage):
    ("SocketManager().bind() should get by name, register then "
     "bind to the given address.")

    # Given a zmq mock
    zmq = Mock()

    # And a context
    context = Mock()

    # And a socket manager
    manager = SocketManager(zmq, context)

    # When I call bind
    result = manager.bind('foobar', 'inproc://whatevs', 'some-mechanism')

    # Then it should have returned a socket from get_by_name
    result.should.equal(get_by_name.return_value)

    # And get_by_name should have been called with the name
    get_by_name.assert_called_once_with('foobar')

    # And register_socket should have been called
    register_socket.assert_called_once_with(
        result,
        "some-mechanism"
    )

    # And it should have called bind on the given address
    result.bind.assert_called_once_with('inproc://whatevs')

    # And it should have engaged the manager with zero timeout
    engage.assert_called_once_with(0)
开发者ID:adamchainz,项目名称:agentzero,代码行数:35,代码来源:test_socket_manager.py

示例2: Pipeline

# 需要导入模块: from agentzero.core import SocketManager [as 别名]
# 或者: from agentzero.core.SocketManager import bind [as 别名]
class Pipeline(object):
    def __init__(self, name, steps=[]):
        self.name = name
        self.actions = Speaker(
            'actions',
            [
                'available',
                'failed',
                'started',
                'success',
                'metric',
                'error',
                'logs',
            ]
        )
        self.steps = [s.job_type for s in steps]
        self.total_steps = len(steps)
        self.context = zmq.Context()
        self.sockets = SocketManager(zmq, self.context)
        self.sockets.create('step-events', zmq.SUB)
        self.sockets.create('jobs-in', zmq.PULL)
        for step in self.steps:
            self.sockets.create(step, zmq.PUSH)

        for action in self.actions.actions.keys():
            self.bind_action(action)

        self.total_actions = len(self.actions.actions)
        self.pool = gevent.pool.Pool(self.total_actions ** (self.total_steps + 1))
        self.greenlets = []
        self._allowed_to_run = True
        self.default_interval = 0.1
        self.backend = StorageBackend()
        self.logger = logging.getLogger('pipeline')

    def on_started(self, event):
        worker = Worker.from_event(event)
        self.logger.info('%s [%s] started to process a job', worker.job_type, worker.id)

    def on_available(self, event):
        worker = Worker.from_event(event)
        if self.backend.register_worker(worker):
            self.sockets.connect(worker.job_type, worker.address, zmq.POLLOUT)
            self.logger.info('connected to worker: [%s]', dict(worker))

    def on_failed(self, event):
        worker = Worker.from_event(event)
        self.logger.warning('%s [%s] failed', worker.job_type, worker.id)

    def on_success(self, event):
        worker = Worker.from_event(event)
        self.logger.info('%s [%s] success', worker.job_type, worker.id)
        self.enqueue_next_job(event.data)

    def on_metric(self, event):
        self.logger.info(' '.join([event.topic, event.data]))

    def on_error(self, event):
        worker = Worker.from_event(event)
        self.logger.warning('%s [%s] errored: %s', worker.job_type, worker.id, event)

    def on_logs(self, event):
        msg = event.data.pop('msg', None)
        if msg:
            self.logger.debug(msg)

    def enqueue_next_job(self, data):
        result = data.pop('instructions')
        job = Job.from_dict(data)
        job['instructions'] = result

        step_index = self.steps.index(job.type)

        try:
            next_job_type = self.steps[step_index + 1]
        except IndexError:
            next_job_type = None

        if next_job_type:
            self.logger.info("enqueuing next job: %s", next_job_type)
            job['job_type'] = next_job_type
            self.backend.enqueue_job(job)

    def bind_action(self, name, method=None):
        action = getattr(self.actions, name, None)
        if not action:
            raise KeyError('undefined action: {0}'.format(name))

        method = method or getattr(self, 'on_{0}'.format(name), None)
        if not method:
            raise TypeError('{0} does not have method {1}(self, topic, data)'.format(self.__class__, name))

        action(lambda _, event: self.spawn(method, event))

    def should_run(self):
        return self._allowed_to_run

    def listen(self, subscriber_bind_address='tcp://127.0.0.1:6000', pull_bind_address='tcp://127.0.0.1:7000'):
        self.sockets.bind('step-events', subscriber_bind_address, zmq.POLLIN)
        self.sockets.bind('jobs-in', pull_bind_address, zmq.POLLIN)
#.........这里部分代码省略.........
开发者ID:gabrielfalcao,项目名称:agentzero,代码行数:103,代码来源:engine.py


注:本文中的agentzero.core.SocketManager.bind方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。