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


Python ThreadedReactor.call_now方法代码示例

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


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

示例1: setup

# 需要导入模块: from minitwisted import ThreadedReactor [as 别名]
# 或者: from minitwisted.ThreadedReactor import call_now [as 别名]
class TestMinitwisted:

    def setup(self):
        global time
        #TODO: mock time and socket
        #time = minitwisted.time = MockTime()
        #minitwisted.socket = MockSocket()
        
        self.lock = threading.Lock()
        self.datagrams_received = []
        self.callback_order = []
        self.client_r = ThreadedReactor(task_interval=tc.TASK_INTERVAL)
        self.server_r = ThreadedReactor(task_interval=tc.TASK_INTERVAL)
        self.client_s = self.client_r.listen_udp(tc.CLIENT_ADDR[1],
                                                 self.on_datagram_received)
        self.server_s = self.server_r.listen_udp(tc.SERVER_ADDR[1],
                                                 self.on_datagram_received)
        self.client_r.start()
        self.server_r.start()

    def test_listen_upd(self):
        r = ThreadedReactor()
        r.start()
        logger.warning(''.join(
            ('TESTING LOGS ** IGNORE EXPECTED WARNING ** ',
             '(udp_listen has not been called)')))
        self.client_r.sendto(DATA, tc.SERVER_ADDR)
        while 1: #waiting for data
            with self.lock:
                if self.datagrams_received:
                    break
            time.sleep(tc.TASK_INTERVAL)
        with self.lock:
            first_datagram = self.datagrams_received.pop(0)
            logger.debug('first_datagram: %s, %s' % (
                    first_datagram,
                    (DATA, tc.CLIENT_ADDR)))
            assert first_datagram, (DATA, tc.CLIENT_ADDR)
        r.stop()
            
    def test_network_callback(self):
        self.client_r.sendto(DATA, tc.SERVER_ADDR)
        time.sleep(tc.TASK_INTERVAL)
        with self.lock:
            first_datagram = self.datagrams_received.pop(0)
            logger.debug('first_datagram: %s, %s' % (
                    first_datagram,
                    (DATA, tc.CLIENT_ADDR)))
            assert first_datagram, (DATA, tc.CLIENT_ADDR)

    def test_block_flood(self):
        from floodbarrier import MAX_PACKETS_PER_PERIOD as FLOOD_LIMIT

        for _ in xrange(FLOOD_LIMIT):
            self.client_r.sendto(DATA, tc.SERVER_ADDR)
        for _ in xrange(10):
            self.client_r.sendto(DATA, tc.SERVER_ADDR)
            logger.warning(
                "TESTING LOGS ** IGNORE EXPECTED WARNING **")
            time.sleep(tc.TASK_INTERVAL)
        return
######################################
        with self.lock:
            logger.debug('datagram processed: %d/%d' % (
                              len(self.datagrams_received),
                              FLOOD_LIMIT))
            print len(self.datagrams_received)
            assert len(self.datagrams_received) <= FLOOD_LIMIT

    def test_call_later(self):
        self.client_r.call_later(.13, self.callback_f, 1)
        self.client_r.call_later(.11, self.callback_f, 2)
        self.client_r.call_later(.01, self.callback_f, 3)
        task4 = self.client_r.call_later(.01, self.callback_f, 4)
        task4.cancel()
        time.sleep(.03)
        with self.lock:
            logger.debug('callback_order: %s' % self.callback_order)
            eq_(self.callback_order, [3])
            self.callback_order = []
        self.client_r.call_now(self.callback_f, 5)
        time.sleep(.03)
        with self.lock:
            logger.debug('callback_order: %s' % self.callback_order)
            eq_(self.callback_order, [5])
            self.callback_order = []
        task6 = self.client_r.call_later(.03, self.callback_f, 6)
        task6.cancel()
        time.sleep(.1)
        with self.lock:
            logger.debug('callback_order: %s' % self.callback_order)
            eq_(self.callback_order, [2, 1])

    def test_network_and_delayed(self):
        self.client_r.call_later(.2, self.callback_f, 0)
        self.client_r.call_now(self.callback_f, 1)
        task2 = self.client_r.call_later(.2, self.callback_f, 2)
        with self.lock:
            eq_(self.callback_order, [])
        time.sleep(.1)
#.........这里部分代码省略.........
开发者ID:csasm,项目名称:Look-MLKademlia,代码行数:103,代码来源:test_minitwisted.py


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