本文整理汇总了Python中stream.Stream.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Python Stream.subscribe方法的具体用法?Python Stream.subscribe怎么用?Python Stream.subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Stream
的用法示例。
在下文中一共展示了Stream.subscribe方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestPyMorseStreamSlow
# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import subscribe [as 别名]
class TestPyMorseStreamSlow(unittest.TestCase):
def setUp(self):
print("Starting test...")
self.freq = 10
self._server = SocketWriter(freq = self.freq) # First tests: producer at 10 Hz
self.com = ComChannel("localhost")
self.stream = Stream(self.com, 61000, maxlength = 3)
def test_get(self):
self.assertEqual(self.stream.get(), [0])
self.assertEqual(self.stream.get(), [1])
def test_last(self):
self.assertIsNone(self.stream.last())
self.stream.get()
self.assertEqual(self.stream.last(), [0])
self.stream.get()
self.assertEqual(self.stream.last(), [1])
def test_last_timed(self):
d = 1/float(self.freq)
self.assertIsNone(self.stream.last())
time.sleep(d + d * 0.5)
self.assertEqual(self.stream.last(), [0])
time.sleep(d)
self.assertEqual(self.stream.last(), [1])
time.sleep(d)
time.sleep(d)
self.assertEqual(self.stream.last(), [3])
def test_last_n(self):
d = 1/float(self.freq)
self.assertIsNone(self.stream.last())
self.assertIsNone(self.stream.last(2))
self.assertIsNone(self.stream.last(10))
time.sleep(d + d * 0.5)
self.assertEqual(self.stream.last(1), [[0]])
self.assertEqual(self.stream.last(2), [[0]]) # if only one item available, should be fine to ask for two
time.sleep(d)
self.assertEqual(self.stream.last(1), [[1]]) # make sure we return the last one
self.assertEqual(self.stream.last(2), [[0],[1]]) # make sure the order is right (from older to most recent)
time.sleep(d)
self.assertEqual(self.stream.last(3), [[0],[1],[2]])
time.sleep(d)
self.assertEqual(self.stream.last(3), [[1], [2],[3]])
self.assertEqual(self.stream.last(4), [[1], [2],[3]]) # make sure the buffer lenght (here, 3) is used
def test_subscribe(self):
d = 1/float(self.freq)
self.ok = False
self.i = 0
self.stream.subscribe(self.on_data)
time.sleep(d + d * 0.5)
self.assertTrue(self.ok)
self.ok = False
time.sleep(d)
self.assertTrue(self.ok)
def test_unsubscribe(self):
d = 1/float(self.freq)
self.ok = False
self.i = 0
self.stream.subscribe(self.on_data)
time.sleep(d + d * 0.5)
self.assertTrue(self.ok)
self.ok = False
self.stream.unsubscribe(self.on_data)
time.sleep(d)
self.assertFalse(self.ok)
def on_data(self, record):
self.ok = True
self.assertEqual(record, [self.i])
self.i += 1
def tearDown(self):
self.com.close()
#.........这里部分代码省略.........