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


Python WorkQueue.enqueue方法代码示例

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


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

示例1: SandeshStateMachine

# 需要导入模块: from work_queue import WorkQueue [as 别名]
# 或者: from work_queue.WorkQueue import enqueue [as 别名]
class SandeshStateMachine(object):

    _IDLE_HOLD_TIME = 5 # in seconds
    _CONNECT_TIME = 30 # in seconds

    def __init__(self, connection, logger, primary_collector, 
                 secondary_collector):

        def _on_idle(e):
            if e.sm._connect_timer is not None:
                e.sm._cancel_connect_timer()
            # Reset active and backup collector
            self._active_collector = self._connection.primary_collector()
            self._backup_collector = self._connection.secondary_collector()
            # clean up existing connection
            e.sm._delete_session()
            e.sm._start_idle_hold_timer()
        #end _on_idle

        def _on_disconnect(e):
            pass
        #end _on_disconnect

        def _on_connect(e):
            if e.sm._idle_hold_timer is not None:
                e.sm._cancel_idle_hold_timer()
            e.sm._connection.reset_collector()
            # clean up existing connection
            e.sm._delete_session()
            if e.sm._active_collector is not None:
                e.sm._create_session()
                e.sm._start_connect_timer()
                e.sm._session.connect()
            else:
                e.sm.enqueue_event(Event(event = Event._EV_COLLECTOR_UNKNOWN))
        #end _on_connect

        def _on_connect_to_backup(e):
            if e.sm._connect_timer is not None:
                e.sm._cancel_connect_timer()
            # clean up existing connection
            e.sm._delete_session()
            # try to connect to the backup collector, if known
            if e.sm._backup_collector is not None:
                e.sm._active_collector, e.sm._backup_collector = \
                    e.sm._backup_collector, e.sm._active_collector
                e.sm._create_session()
                e.sm._start_connect_timer()
                e.sm._session.connect()
            else:
                e.sm.enqueue_event(Event(event = Event._EV_BACKUP_COLLECTOR_UNKNOWN))
        #end _on_connect_to_backup

        def _on_client_init(e):
            e.sm._connects += 1
            gevent.spawn(e.sm._session.read)
            e.sm._connection.handle_initialized(e.sm._connects)
            e.sm._connection.sandesh_instance().send_generator_info()
        #end _on_client_init
        
        def _on_established(e):
            e.sm._cancel_connect_timer()
            e.sm._connection.set_collector(e.sm_event.source)
            e.sm._connection.handle_sandesh_ctrl_msg(e.sm_event.msg)
            self._connection.sandesh_instance().send_generator_info()
        #end _on_established

        # FSM - Fysom
        self._fsm = Fysom({
                           'initial': {'state' : State._IDLE,
                                       'event' : Event._EV_START,
                                       'defer' : True
                                      },
                           'events': [
                                      # _IDLE
                                      {'name' : Event._EV_IDLE_HOLD_TIMER_EXPIRED,
                                       'src'  : State._IDLE,
                                       'dst'  : State._CONNECT
                                      },
                                      {'name' : Event._EV_COLLECTOR_CHANGE,
                                       'src'  : State._IDLE,
                                       'dst'  : State._CONNECT
                                      },

                                      # _DISCONNECT 
                                      {'name' : Event._EV_COLLECTOR_CHANGE,
                                       'src'  : State._DISCONNECT,
                                       'dst'  : State._CONNECT
                                      },

                                      # _CONNECT
                                      {'name' : Event._EV_COLLECTOR_UNKNOWN,
                                       'src'  : State._CONNECT,
                                       'dst'  : State._DISCONNECT
                                      },
                                      {'name' : Event._EV_TCP_CONNECT_FAIL,
                                       'src'  : State._CONNECT,
                                       'dst'  : State._CONNECT_TO_BACKUP
                                      },
                                      {'name' : Event._EV_CONNECT_TIMER_EXPIRED,
#.........这里部分代码省略.........
开发者ID:routelastresort,项目名称:contrail-sandesh,代码行数:103,代码来源:sandesh_state_machine.py

示例2: SandeshSession

# 需要导入模块: from work_queue import WorkQueue [as 别名]
# 或者: from work_queue.WorkQueue import enqueue [as 别名]
class SandeshSession(TcpSession):
    _KEEPALIVE_IDLE_TIME = 45  # in secs
    _KEEPALIVE_INTERVAL = 3  # in secs
    _KEEPALIVE_PROBES = 5

    def __init__(self, sandesh_instance, server, event_handler, sandesh_msg_handler):
        self._sandesh_instance = sandesh_instance
        self._logger = sandesh_instance._logger
        self._event_handler = event_handler
        self._reader = SandeshReader(self, sandesh_msg_handler)
        self._writer = SandeshWriter(self)
        self._send_queue = WorkQueue(self._send_sandesh, self._is_ready_to_send_sandesh)
        TcpSession.__init__(self, server)

    # end __init__

    # Public functions

    def sandesh_instance(self):
        return self._sandesh_instance

    # end sandesh_instance

    def is_send_queue_empty(self):
        return self._send_queue.is_queue_empty()

    # end is_send_queue_empty

    def is_connected(self):
        return self._connected

    # end is_connected

    def enqueue_sandesh(self, sandesh):
        self._send_queue.enqueue(sandesh)

    # end enqueue_sandesh

    def send_queue(self):
        return self._send_queue

    # end send_queue

    # Overloaded functions from TcpSession

    def connect(self):
        TcpSession.connect(self, timeout=5)

    # end connect

    def _on_read(self, buf):
        if self._reader.read_msg(buf) < 0:
            self._logger.error("SandeshReader Error. Close Collector session")
            self.close()

    # end _on_read

    def _handle_event(self, event):
        self._event_handler(self, event)

    # end _handle_event

    def _set_socket_options(self):
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        if hasattr(socket, "TCP_KEEPIDLE"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, self._KEEPALIVE_IDLE_TIME)
        if hasattr(socket, "TCP_KEEPALIVE"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, self._KEEPALIVE_IDLE_TIME)
        if hasattr(socket, "TCP_KEEPINTVL"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, self._KEEPALIVE_INTERVAL)
        if hasattr(socket, "TCP_KEEPCNT"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, self._KEEPALIVE_PROBES)

    # end _set_socket_options

    # Private functions

    def _send_sandesh(self, sandesh):
        if self._send_queue.is_queue_empty():
            more = False
        else:
            more = True
        if not self._connected:
            self._logger.log(SandeshLogger.get_py_logger_level(sandesh.level()), sandesh.log())
            return
        if sandesh.is_logging_allowed(self._sandesh_instance):
            self._logger.log(SandeshLogger.get_py_logger_level(sandesh.level()), sandesh.log())
        self._writer.send_msg(sandesh, more)

    # end _send_sandesh

    def _is_ready_to_send_sandesh(self):
        return self._sandesh_instance.is_send_queue_enabled()
开发者ID:gdpak,项目名称:contrail-sandesh,代码行数:95,代码来源:sandesh_session.py

示例3: Sandesh

# 需要导入模块: from work_queue import WorkQueue [as 别名]
# 或者: from work_queue.WorkQueue import enqueue [as 别名]

#.........这里部分代码省略.........
    #end is_syslog_logging_enabled

    def logging_syslog_facility(self):
        return self._sandesh_logger.logging_syslog_facility()
    #end logging_syslog_facility

    def is_unit_test(self):
        return self._role == self.SandeshRole.INVALID
    # end is_unit_test

    def handle_test(self, sandesh_init):
        if sandesh_init.is_unit_test() or self._is_level_ut():
            if self._is_logging_allowed(sandesh_init):
                sandesh_init._logger.debug(self.log())
                return True
        return False

    def is_logging_allowed(self, sandesh_init):
        if not sandesh_init.is_local_logging_enabled():
            return False

        logging_level = sandesh_init.logging_level()
        level_allowed = logging_level >= self._level

        logging_category = sandesh_init.logging_category()
        if logging_category is None or len(logging_category) == 0:
            category_allowed = True
        else:
            category_allowed = logging_category == self._category

        return level_allowed and category_allowed
    # end is_logging_allowed

    def enqueue_sandesh_request(self, sandesh):
        self._rcv_queue.enqueue(sandesh)
    # end enqueue_sandesh_request

    def send_sandesh(self, tx_sandesh):
        if self._client:
            ret = self._client.send_sandesh(tx_sandesh)
        else:
            self._logger.debug(tx_sandesh.log())
    # end send_sandesh

    def send_generator_info(self):
        from gen_py.sandesh_uve.ttypes import SandeshClientInfo, \
            ModuleClientState, SandeshModuleClientTrace
        client_info = SandeshClientInfo()
        try:
            client_start_time = self._start_time
        except:
            self._start_time = UTCTimestampUsec()
        finally:
            client_info.start_time = self._start_time
            client_info.pid = os.getpid()
            if self._http_server is not None:
                client_info.http_port = self._http_server.get_port()
            client_info.collector_name = self._client.connection().collector()
            client_info.status = self._client.connection().state()
            client_info.successful_connections = \
                self._client.connection().statemachine().connect_count()
            client_info.primary = self._client.connection().primary_collector()
            if client_info.primary is None:
                client_info.primary = ''
            client_info.secondary = \
                self._client.connection().secondary_collector()
开发者ID:nischalsheth,项目名称:contrail-sandesh,代码行数:70,代码来源:sandesh_base.py

示例4: Sandesh

# 需要导入模块: from work_queue import WorkQueue [as 别名]
# 或者: from work_queue.WorkQueue import enqueue [as 别名]

#.........这里部分代码省略.........
        return self._sandesh_logger.logging_syslog_facility()
    # end logging_syslog_facility

    def is_unit_test(self):
        return self._role == self.SandeshRole.INVALID
    # end is_unit_test

    def handle_test(self, sandesh_init):
        if sandesh_init.is_unit_test() or self._is_level_ut():
            if self.is_logging_allowed(sandesh_init):
                sandesh_init._logger.debug(self.log())
            return True
        return False

    def is_logging_allowed(self, sandesh_init):
        if self._type == SandeshType.FLOW:
            return sandesh_init.is_flow_logging_enabled()

        if not sandesh_init.is_local_logging_enabled():
            return False

        logging_level = sandesh_init.logging_level()
        level_allowed = logging_level >= self._level

        logging_category = sandesh_init.logging_category()
        if logging_category is None or len(logging_category) == 0:
            category_allowed = True
        else:
            category_allowed = logging_category == self._category

        return level_allowed and category_allowed
    # end is_logging_allowed

    def enqueue_sandesh_request(self, sandesh):
        self._rcv_queue.enqueue(sandesh)
    # end enqueue_sandesh_request

    def send_sandesh(self, tx_sandesh):
        if self._client:
            self._client.send_sandesh(tx_sandesh)
        else:
            if self._connect_to_collector:
                self.drop_tx_sandesh(tx_sandesh, SandeshTxDropReason.NoClient)
            else:
                self.drop_tx_sandesh(tx_sandesh, SandeshTxDropReason.NoClient,
                    tx_sandesh.level())
    # end send_sandesh

    def drop_tx_sandesh(self, tx_sandesh, drop_reason, level=None):
        self._msg_stats.update_tx_stats(tx_sandesh.__class__.__name__,
            sys.getsizeof(tx_sandesh), drop_reason)
        if self.is_logging_dropped_allowed(tx_sandesh):
            if level is not None:
                self._logger.log(
                    sand_logger.SandeshLogger.get_py_logger_level(level),
                    tx_sandesh.log())
            else:
                self._logger.error('SANDESH: [DROP: %s] %s' % \
                    (SandeshTxDropReason._VALUES_TO_NAMES[drop_reason],
                     tx_sandesh.log()))
    # end drop_tx_sandesh

    def send_generator_info(self):
        from gen_py.sandesh_uve.ttypes import SandeshClientInfo, \
            ModuleClientState, SandeshModuleClientTrace
        client_info = SandeshClientInfo()
开发者ID:th3architect,项目名称:contrail-sandesh,代码行数:70,代码来源:sandesh_base.py

示例5: SandeshStateMachine

# 需要导入模块: from work_queue import WorkQueue [as 别名]
# 或者: from work_queue.WorkQueue import enqueue [as 别名]
class SandeshStateMachine(object):

    _IDLE_HOLD_TIME = 4 # in seconds
    _CONNECT_TIME = 30 # in seconds

    def __init__(self, connection, logger, collectors):

        def _update_connection_state(e, status):
            from connection_info import ConnectionState
            from gen_py.process_info.ttypes import ConnectionType
            collector_addr = e.sm.collector()
            if collector_addr is None:
                collector_addr = ''
            ConnectionState.update(conn_type = ConnectionType.COLLECTOR,
                name = '',
                status = status,
                server_addrs = [collector_addr],
                message = '%s to %s on %s' % (e.src, e.dst, e.event))
        #end _update_connection_state

        def _connection_state_up(e):
            from gen_py.process_info.ttypes import ConnectionStatus
            _update_connection_state(e, ConnectionStatus.UP)
        #end _connection_state_up

        def _connection_state_down(e):
            from gen_py.process_info.ttypes import ConnectionStatus
            _update_connection_state(e, ConnectionStatus.DOWN)
        #end _connection_state_down

        def _connection_state_init(e):
            from gen_py.process_info.ttypes import ConnectionStatus
            _update_connection_state(e, ConnectionStatus.INIT)
        #end _connection_state_init

        def _on_idle(e):
            if e.sm._connect_timer is not None:
                e.sm._cancel_connect_timer()
            # clean up existing connection
            e.sm._delete_session()
            if e.sm._disable != True:
	        e.sm._start_idle_hold_timer()
            # update connection state
            _connection_state_down(e)
            e.sm._collector_name = None
            e.sm._connection.sandesh_instance().send_generator_info()
        #end _on_idle

        def _on_disconnect(e):
            # update connection state
            _connection_state_down(e)
        #end _on_disconnect

        def _on_connect(e):
            if e.sm._idle_hold_timer is not None:
                e.sm._cancel_idle_hold_timer()
            e.sm._collector_name = None
            # clean up existing connection
            e.sm._delete_session()
            collector = e.sm._get_next_collector()
            if collector is not None:
                # update connection state
                _connection_state_init(e)
                e.sm._create_session(collector)
                e.sm._start_connect_timer()
                e.sm._session.connect()
            else:
                e.sm.enqueue_event(Event(event = Event._EV_COLLECTOR_UNKNOWN))
        #end _on_connect

        def _on_client_init(e):
            e.sm._connects += 1
            gevent.spawn(e.sm._session.read)
            e.sm._connection.handle_initialized(e.sm._connects)
            e.sm._connection.sandesh_instance().send_generator_info()
            # update connection state
            _connection_state_init(e)
        #end _on_client_init
        
        def _on_established(e):
            e.sm._cancel_connect_timer()
            e.sm._collector_name = e.sm_event.source
            e.sm._connection.handle_sandesh_ctrl_msg(e.sm_event.msg)
            e.sm._connection.sandesh_instance().send_generator_info()
            # update connection state
            _connection_state_up(e)
        #end _on_established

        # FSM - Fysom
        self._fsm = Fysom({
                           'initial': {'state' : State._IDLE,
                                       'event' : Event._EV_START,
                                       'defer' : True
                                      },
                           'events': [
                                      # _IDLE
                                      {'name' : Event._EV_IDLE_HOLD_TIMER_EXPIRED,
                                       'src'  : State._IDLE,
                                       'dst'  : State._CONNECT
                                      },
#.........这里部分代码省略.........
开发者ID:Juniper,项目名称:contrail-sandesh,代码行数:103,代码来源:sandesh_state_machine.py

示例6: SandeshSession

# 需要导入模块: from work_queue import WorkQueue [as 别名]
# 或者: from work_queue.WorkQueue import enqueue [as 别名]
class SandeshSession(TcpSession):
    _KEEPALIVE_IDLE_TIME = 15  # in secs
    _KEEPALIVE_INTERVAL = 3  # in secs
    _KEEPALIVE_PROBES = 5
    _TCP_USER_TIMEOUT_OPT = 18
    _TCP_USER_TIMEOUT_VAL = 30000  # ms

    def __init__(self, sandesh_instance, server, event_handler, sandesh_msg_handler):
        self._sandesh_instance = sandesh_instance
        self._logger = sandesh_instance._logger
        self._event_handler = event_handler
        self._reader = SandeshReader(self, sandesh_msg_handler)
        self._writer = SandeshWriter(self)
        self._send_queue = WorkQueue(self._send_sandesh, self._is_ready_to_send_sandesh)
        TcpSession.__init__(self, server)

    # end __init__

    # Public functions

    def sandesh_instance(self):
        return self._sandesh_instance

    # end sandesh_instance

    def is_send_queue_empty(self):
        return self._send_queue.is_queue_empty()

    # end is_send_queue_empty

    def is_connected(self):
        return self._connected

    # end is_connected

    def enqueue_sandesh(self, sandesh):
        self._send_queue.enqueue(sandesh)

    # end enqueue_sandesh

    def send_queue(self):
        return self._send_queue

    # end send_queue

    # Overloaded functions from TcpSession

    def connect(self):
        TcpSession.connect(self, timeout=5)

    # end connect

    def _on_read(self, buf):
        if self._reader.read_msg(buf) < 0:
            self._logger.error("SandeshReader Error. Close Collector session")
            self.close()

    # end _on_read

    def _handle_event(self, event):
        self._event_handler(self, event)

    # end _handle_event

    def _set_socket_options(self):
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        if hasattr(socket, "TCP_KEEPIDLE"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, self._KEEPALIVE_IDLE_TIME)
        if hasattr(socket, "TCP_KEEPALIVE"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, self._KEEPALIVE_IDLE_TIME)
        if hasattr(socket, "TCP_KEEPINTVL"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, self._KEEPALIVE_INTERVAL)
        if hasattr(socket, "TCP_KEEPCNT"):
            self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, self._KEEPALIVE_PROBES)
        try:
            self._socket.setsockopt(socket.IPPROTO_TCP, self._TCP_USER_TIMEOUT_OPT, self._TCP_USER_TIMEOUT_VAL)
        except:
            self._logger.error(
                "setsockopt failed: option %d, value %d" % (self._TCP_USER_TIMEOUT_OPT, self._TCP_USER_TIMEOUT_VAL)
            )

    # end _set_socket_options

    # Private functions

    def _send_sandesh(self, sandesh):
        if self._send_queue.is_queue_empty():
            more = False
        else:
            more = True
        if not self._connected:
            if self._sandesh_instance.is_logging_dropped_allowed(sandesh):
                self._logger.error("SANDESH: %s: %s" % ("Not connected", sandesh.log()))
            self._sandesh_instance.msg_stats().update_tx_stats(
                sandesh.__class__.__name__, 0, SandeshTxDropReason.SessionNotConnected
            )
            return
        if sandesh.is_logging_allowed(self._sandesh_instance):
            self._logger.log(SandeshLogger.get_py_logger_level(sandesh.level()), sandesh.log())
        self._writer.send_msg(sandesh, more)
#.........这里部分代码省略.........
开发者ID:arvindvis,项目名称:contrail-sandesh,代码行数:103,代码来源:sandesh_session.py


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