• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python read_preferences.MovingAverage类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pymongo.read_preferences.MovingAverage的典型用法代码示例。如果您正苦于以下问题:Python MovingAverage类的具体用法?Python MovingAverage怎么用?Python MovingAverage使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: test_moving_average

 def test_moving_average(self):
     avg = MovingAverage([10])
     self.assertEqual(10, avg.get())
     avg2 = avg.clone_with(20)
     self.assertEqual(15, avg2.get())
     avg3 = avg2.clone_with(30)
     self.assertEqual(20, avg3.get())
     avg4 = avg3.clone_with(-100)
     self.assertEqual((10 + 20 + 30 - 100) / 4., avg4.get())
     avg5 = avg4.clone_with(17)
     self.assertEqual((10 + 20 + 30 - 100 + 17) / 5., avg5.get())
     avg6 = avg5.clone_with(43)
     self.assertEqual((20 + 30 - 100 + 17 + 43) / 5., avg6.get())
     avg7 = avg6.clone_with(-1111)
     self.assertEqual((30 - 100 + 17 + 43 - 1111) / 5., avg7.get())
开发者ID:easonlin,项目名称:mongo-python-driver,代码行数:15,代码来源:test_read_preferences.py


示例2: run_scenario

    def run_scenario(self):
        moving_average = MovingAverage()

        if scenario_def['avg_rtt_ms'] != "NULL":
            moving_average.add_sample(scenario_def['avg_rtt_ms'])

        if scenario_def['new_rtt_ms'] != "NULL":
            moving_average.add_sample(scenario_def['new_rtt_ms'])

        self.assertAlmostEqual(moving_average.get(),
                               scenario_def['new_avg_rtt'])
开发者ID:llvtt,项目名称:mongo-python-driver,代码行数:11,代码来源:test_server_selection_rtt.py


示例3: __init__

    def __init__(
            self,
            server_description,
            topology,
            pool,
            topology_settings):
        """Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        """
        self._server_description = server_description
        self._pool = pool
        self._settings = topology_settings
        self._avg_round_trip_time = MovingAverage()
        self._listeners = self._settings._pool_options.event_listeners
        pub = self._listeners is not None
        self._publish = pub and self._listeners.enabled_for_server_heartbeat

        # We strongly reference the executor and it weakly references us via
        # this closure. When the monitor is freed, stop the executor soon.
        def target():
            monitor = self_ref()
            if monitor is None:
                return False  # Stop the executor.
            Monitor._run(monitor)
            return True

        executor = periodic_executor.PeriodicExecutor(
            interval=self._settings.heartbeat_frequency,
            min_interval=common.MIN_HEARTBEAT_INTERVAL,
            target=target,
            name="pymongo_server_monitor_thread")

        self._executor = executor

        # Avoid cycles. When self or topology is freed, stop executor soon.
        self_ref = weakref.ref(self, executor.close)
        self._topology = weakref.proxy(topology, executor.close)
开发者ID:Joseph7117,项目名称:Suggestion_Box,代码行数:42,代码来源:monitor.py


示例4: __init__

    def __init__(
            self,
            server_description,
            topology,
            pool,
            topology_settings):
        """Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        """
        self._server_description = server_description

        # A weakref callback, takes ref to the freed topology as its parameter.
        def close(dummy):
            self.close()

        self._topology = weakref.proxy(topology, close)
        self._pool = pool
        self._settings = topology_settings
        self._avg_round_trip_time = MovingAverage()

        # We strongly reference the executor and it weakly references us via
        # this closure. When the monitor is freed, stop the executor soon.
        self_ref = weakref.ref(self, close)

        def target():
            monitor = self_ref()
            if monitor is None:
                return False  # Stop the executor.
            Monitor._run(monitor)
            return True

        self._executor = periodic_executor.PeriodicExecutor(
            condition_class=self._settings.condition_class,
            interval=common.HEARTBEAT_FREQUENCY,
            min_interval=common.MIN_HEARTBEAT_INTERVAL,
            target=target)
开发者ID:llvtt,项目名称:mongo-python-driver,代码行数:41,代码来源:monitor.py


示例5: test_5_sample_moving_average

 def test_5_sample_moving_average(self):
     avg = MovingAverage(5)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(10, avg.get())
     avg.update(20)
     self.assertEqual(15, avg.get())
     avg.update(30)
     self.assertEqual(20, avg.get())
     avg.update(-100)
     self.assertEqual((10 + 20 + 30 - 100) / 4, avg.get())
     avg.update(17)
     self.assertEqual((10 + 20 + 30 - 100 + 17) / 5., avg.get())
     avg.update(43)
     self.assertEqual((20 + 30 - 100 + 17 + 43) / 5., avg.get())
     avg.update(-1111)
     self.assertEqual((30 - 100 + 17 + 43 - 1111) / 5., avg.get())
开发者ID:bussiere,项目名称:mongo-python-driver,代码行数:17,代码来源:test_read_preferences.py


示例6: test_2_sample_moving_average

 def test_2_sample_moving_average(self):
     avg = MovingAverage(2)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(10, avg.get())
     avg.update(20)
     self.assertEqual(15, avg.get())
     avg.update(30)
     self.assertEqual(25, avg.get())
     avg.update(-100)
     self.assertEqual(-35, avg.get())
开发者ID:bussiere,项目名称:mongo-python-driver,代码行数:11,代码来源:test_read_preferences.py


示例7: test_trivial_moving_average

 def test_trivial_moving_average(self):
     avg = MovingAverage(1)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(10, avg.get())
     avg.update(20)
     self.assertEqual(20, avg.get())
     avg.update(0)
     self.assertEqual(0, avg.get())
开发者ID:bussiere,项目名称:mongo-python-driver,代码行数:9,代码来源:test_read_preferences.py


示例8: test_empty_moving_average

 def test_empty_moving_average(self):
     avg = MovingAverage(0)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(None, avg.get())
开发者ID:bussiere,项目名称:mongo-python-driver,代码行数:5,代码来源:test_read_preferences.py


示例9: Monitor

class Monitor(object):
    def __init__(
            self,
            server_description,
            topology,
            pool,
            topology_settings):
        """Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        """
        self._server_description = server_description
        self._pool = pool
        self._settings = topology_settings
        self._avg_round_trip_time = MovingAverage()

        # We strongly reference the executor and it weakly references us via
        # this closure. When the monitor is freed, stop the executor soon.
        def target():
            monitor = self_ref()
            if monitor is None:
                return False  # Stop the executor.
            Monitor._run(monitor)
            return True

        executor = periodic_executor.PeriodicExecutor(
            interval=common.HEARTBEAT_FREQUENCY,
            min_interval=common.MIN_HEARTBEAT_INTERVAL,
            target=target,
            name="pymongo_server_monitor_thread")

        self._executor = executor
        
        # Avoid cycles. When self or topology is freed, stop executor soon.
        self_ref = weakref.ref(self, executor.close)
        self._topology = weakref.proxy(topology, executor.close)

    def open(self):
        """Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        """
        self._executor.open()

    def close(self):
        """Close and stop monitoring.

        open() restarts the monitor after closing.
        """
        self._executor.close()

        # Increment the pool_id and maybe close the socket. If the executor
        # thread has the socket checked out, it will be closed when checked in.
        self._pool.reset()

    def join(self, timeout=None):
        self._executor.join(timeout)

    def request_check(self):
        """If the monitor is sleeping, wake and check the server soon."""
        self._executor.wake()

    def _run(self):
        try:
            self._server_description = self._check_with_retry()
            self._topology.on_change(self._server_description)
        except ReferenceError:
            # Topology was garbage-collected.
            self.close()

    def _check_with_retry(self):
        """Call ismaster once or twice. Reset server's pool on error.

        Returns a ServerDescription.
        """
        # According to the spec, if an ismaster call fails we reset the
        # server's pool. If a server was once connected, change its type
        # to Unknown only after retrying once.
        address = self._server_description.address
        retry = self._server_description.server_type != SERVER_TYPE.Unknown

        try:
            return self._check_once()
        except ReferenceError:
            raise
        except Exception as error:
            self._topology.reset_pool(address)
            default = ServerDescription(address, error=error)
            if not retry:
                self._avg_round_trip_time.reset()
                # Server type defaults to Unknown.
                return default

            # Try a second and final time. If it fails return original error.
            try:
                return self._check_once()
#.........这里部分代码省略.........
开发者ID:Alpus,项目名称:Eth,代码行数:101,代码来源:monitor.py


示例10: test_moving_average

 def test_moving_average(self):
     avg = MovingAverage()
     self.assertIsNone(avg.get())
     avg.add_sample(10)
     self.assertAlmostEqual(10, avg.get())
     avg.add_sample(20)
     self.assertAlmostEqual(12, avg.get())
     avg.add_sample(30)
     self.assertAlmostEqual(15.6, avg.get())
开发者ID:behackett,项目名称:mongo-python-driver,代码行数:9,代码来源:test_read_preferences.py


示例11: Monitor

class Monitor(object):
    def __init__(
            self,
            server_description,
            topology,
            pool,
            topology_settings):
        """Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        """
        self._server_description = server_description
        self._pool = pool
        self._settings = topology_settings
        self._avg_round_trip_time = MovingAverage()
        self._listeners = self._settings._pool_options.event_listeners
        pub = self._listeners is not None
        self._publish = pub and self._listeners.enabled_for_server_heartbeat

        # We strongly reference the executor and it weakly references us via
        # this closure. When the monitor is freed, stop the executor soon.
        def target():
            monitor = self_ref()
            if monitor is None:
                return False  # Stop the executor.
            Monitor._run(monitor)
            return True

        executor = periodic_executor.PeriodicExecutor(
            interval=self._settings.heartbeat_frequency,
            min_interval=common.MIN_HEARTBEAT_INTERVAL,
            target=target,
            name="pymongo_server_monitor_thread")

        self._executor = executor

        # Avoid cycles. When self or topology is freed, stop executor soon.
        self_ref = weakref.ref(self, executor.close)
        self._topology = weakref.proxy(topology, executor.close)

    def open(self):
        """Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        """
        self._executor.open()

    def close(self):
        """Close and stop monitoring.

        open() restarts the monitor after closing.
        """
        self._executor.close()

        # Increment the pool_id and maybe close the socket. If the executor
        # thread has the socket checked out, it will be closed when checked in.
        self._pool.reset()

    def join(self, timeout=None):
        self._executor.join(timeout)

    def request_check(self):
        """If the monitor is sleeping, wake and check the server soon."""
        self._executor.wake()

    def _run(self):
        try:
            self._server_description = self._check_with_retry()
            self._topology.on_change(self._server_description)
        except ReferenceError:
            # Topology was garbage-collected.
            self.close()

    def _check_with_retry(self):
        """Call ismaster once or twice. Reset server's pool on error.

        Returns a ServerDescription.
        """
        # According to the spec, if an ismaster call fails we reset the
        # server's pool. If a server was once connected, change its type
        # to Unknown only after retrying once.
        address = self._server_description.address
        retry = True
        metadata = None
        if self._server_description.server_type == SERVER_TYPE.Unknown:
            retry = False
            metadata = self._pool.opts.metadata

        start = _time()
        try:
            # If the server type is unknown, send metadata with first check.
            return self._check_once(metadata=metadata)
        except ReferenceError:
            raise
        except Exception as error:
            error_time = _time() - start
#.........这里部分代码省略.........
开发者ID:rashed-bishal,项目名称:IoT,代码行数:101,代码来源:monitor.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python replica_set_connection.ReplicaSetConnection类代码示例发布时间:2022-05-27
下一篇:
Python pool.get_socket函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap