本文整理汇总了Python中transitions.Machine.start方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.start方法的具体用法?Python Machine.start怎么用?Python Machine.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transitions.Machine
的用法示例。
在下文中一共展示了Machine.start方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import start [as 别名]
class Broker:
states = ['new', 'starting', 'started', 'not_started', 'stopping', 'stopped', 'not_stopped', 'stopped']
def __init__(self, config=None, loop=None):
"""
:param config: Example Yaml config
listeners:
- default: #Mandatory
max-connections: 50000
type: tcp
- my-tcp-1:
bind: 127.0.0.1:1883
- my-tcp-2:
bind: 1.2.3.4:1883
max-connections: 1000
- my-tcp-ssl-1:
bind: 127.0.0.1:8883
ssl: on
cafile: /some/cafile
capath: /some/folder
capath: certificate data
certfile: /some/certfile
keyfile: /some/key
- my-ws-1:
bind: 0.0.0.0:8080
type: ws
timeout-disconnect-delay: 2
publish-retry-delay: 5
:param loop:
:return:
"""
self.logger = logging.getLogger(__name__)
self.config = _defaults
if config is not None:
self.config.update(config)
self._build_listeners_config(self.config)
if loop is not None:
self._loop = loop
else:
self._loop = asyncio.get_event_loop()
self._servers = dict()
self._init_states()
self._sessions = dict()
self._subscriptions = dict()
self._global_retained_messages = dict()
# Broker statistics initialization
self._stats = dict()
# $SYS tree task handle
self.sys_handle = None
def _build_listeners_config(self, broker_config):
self.listeners_config = dict()
try:
listeners_config = broker_config['listeners']
defaults = listeners_config['default']
for listener in listeners_config:
if listener != 'default':
config = dict(defaults)
config.update(listeners_config[listener])
self.listeners_config[listener] = config
except KeyError as ke:
raise BrokerException("Listener config not found invalid: %s" % ke)
def _init_states(self):
self.transitions = Machine(states=Broker.states, initial='new')
self.transitions.add_transition(trigger='start', source='new', dest='starting')
self.transitions.add_transition(trigger='starting_fail', source='starting', dest='not_started')
self.transitions.add_transition(trigger='starting_success', source='starting', dest='started')
self.transitions.add_transition(trigger='shutdown', source='started', dest='stopping')
self.transitions.add_transition(trigger='stopping_success', source='stopping', dest='stopped')
self.transitions.add_transition(trigger='stopping_failure', source='stopping', dest='not_stopped')
self.transitions.add_transition(trigger='start', source='stopped', dest='starting')
@asyncio.coroutine
def start(self):
try:
self.transitions.start()
self.logger.debug("Broker starting")
except MachineError as me:
self.logger.debug("Invalid method call at this moment: %s" % me)
raise BrokerException("Broker instance can't be started: %s" % me)
# Clear broker stats
self._clear_stats()
try:
# Start network listeners
for listener_name in self.listeners_config:
listener = self.listeners_config[listener_name]
self.logger.info("Binding listener '%s' to %s" % (listener_name, listener['bind']))
# Max connections
try:
max_connections = listener['max_connections']
except KeyError:
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import start [as 别名]
class Broker:
"""
MQTT 3.1.1 compliant broker implementation
:param config: Example Yaml config
:param loop: asyncio loop to use. Defaults to ``asyncio.get_event_loop()`` if none is given
:param plugin_namespace: Plugin namespace to use when loading plugin entry_points. Defaults to ``hbmqtt.broker.plugins``
"""
states = ['new', 'starting', 'started', 'not_started', 'stopping', 'stopped', 'not_stopped', 'stopped']
def __init__(self, config=None, loop=None, plugin_namespace=None):
self.logger = logging.getLogger(__name__)
self.config = _defaults
if config is not None:
self.config.update(config)
self._build_listeners_config(self.config)
if loop is not None:
self._loop = loop
else:
self._loop = asyncio.get_event_loop()
self._servers = dict()
self._init_states()
self._sessions = dict()
self._subscriptions = dict()
self._retained_messages = dict()
self._broadcast_queue = asyncio.Queue(loop=self._loop)
self._broadcast_task = None
# Init plugins manager
context = BrokerContext(self)
context.config = self.config
if plugin_namespace:
namespace = plugin_namespace
else:
namespace = 'hbmqtt.broker.plugins'
self.plugins_manager = PluginManager(namespace, context, self._loop)
def _build_listeners_config(self, broker_config):
self.listeners_config = dict()
try:
listeners_config = broker_config['listeners']
defaults = listeners_config['default']
for listener in listeners_config:
config = dict(defaults)
config.update(listeners_config[listener])
self.listeners_config[listener] = config
except KeyError as ke:
raise BrokerException("Listener config not found invalid: %s" % ke)
def _init_states(self):
self.transitions = Machine(states=Broker.states, initial='new')
self.transitions.add_transition(trigger='start', source='new', dest='starting')
self.transitions.add_transition(trigger='starting_fail', source='starting', dest='not_started')
self.transitions.add_transition(trigger='starting_success', source='starting', dest='started')
self.transitions.add_transition(trigger='shutdown', source='started', dest='stopping')
self.transitions.add_transition(trigger='stopping_success', source='stopping', dest='stopped')
self.transitions.add_transition(trigger='stopping_failure', source='stopping', dest='not_stopped')
self.transitions.add_transition(trigger='start', source='stopped', dest='starting')
@asyncio.coroutine
def start(self):
"""
Start the broker to serve with the given configuration
Start method opens network sockets and will start listening for incoming connections.
This method is a *coroutine*.
"""
try:
self._sessions = dict()
self._subscriptions = dict()
self._retained_messages = dict()
self.transitions.start()
self.logger.debug("Broker starting")
except MachineError as me:
self.logger.warn("[WARN-0001] Invalid method call at this moment: %s" % me)
raise BrokerException("Broker instance can't be started: %s" % me)
yield from self.plugins_manager.fire_event(EVENT_BROKER_PRE_START)
try:
# Start network listeners
for listener_name in self.listeners_config:
listener = self.listeners_config[listener_name]
# Max connections
try:
max_connections = listener['max_connections']
except KeyError:
max_connections = -1
# SSL Context
sc = None
if 'ssl' in listener and listener['ssl'].upper() == 'ON':
try:
sc = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
sc.load_cert_chain(listener['certfile'], listener['keyfile'])
#.........这里部分代码省略.........