Sample

Definition

class broqer.op.Sample(interval: float, error_callback=<broqer.default_error_handler.DefaultErrorHandler object>, loop=None)[source]

Emit the last received value periodically

Parameters:
  • interval – time in seconds between emits
  • error_callback – the error callback to be registered
  • loop – asyncio loop to use

Usage

Emit the last received value periodically

Usage:

>>> import asyncio
>>> from broqer import Subject, op
>>> s = Subject()
>>> sample_publisher = s | op.Sample(0.015)
>>> _d = sample_publisher | op.Sink(print, 'Sample:')
>>> s.emit(1)
Sample: 1
>>> asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.06))
Sample: 1
...
Sample: 1
>>> s.emit((2, 3))
>>> asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.06))
Sample: (2, 3)
...
Sample: (2, 3)
>>> _d2 = sample_publisher | op.Sink(print, 'Sample 2:')
Sample 2: (2, 3)
>>> _d.dispose()
>>> asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.06))
Sample 2: (2, 3)
...
Sample 2: (2, 3)
>>> len(s.subscriptions) # how many subscriber are registered
1
>>> _d2.dispose()
>>> len(s.subscriptions)
0
>>> asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.02))