class CarbonSender(Service): host = "127.0.0.1" # type: str port = 2003 # type: int send_interval = 5 # type: int protocol = "udp" # type: str namespace = ("",) # type: t.Iterable[str] storage = TotalStorage _handle = None # type: PeriodicCallback async def start(self) -> None: namespace = ".".join( strip_carbon_ns(item) for item in self.namespace ) client = PROTOCOLS[self.protocol]( self.host, self.port, namespace=namespace, storage=self.storage(), ) set_client(client) self._handle = PeriodicCallback(client.send) self._handle.start(self.send_interval, loop=self.loop) log.info( "Periodic carbon metrics sender started. Send to %s://%s:%d with " "interval %rs", self.protocol, self.host, self.port, self.send_interval, ) async def stop(self, exc: Exception = None) -> None: self._handle.stop()
class CarbonSender(Service): host = '127.0.0.1' # type: str port = 2003 # type: int send_interval = 5 # type: int protocol = 'udp' # type: str namespace = '' # type: List[str] storage = TotalStorage _handle = None # type: PeriodicCallback async def start(self): namespace = ".".join(strip_carbon_ns(item) for item in self.namespace) client = PROTOCOLS[self.protocol]( self.host, self.port, namespace=namespace, storage=self.storage(), ) set_client(client) self._handle = PeriodicCallback(client.send) self._handle.start(self.send_interval, loop=self.loop) log.info( 'Periodic carbon metrics sender started. Send to %s://%s:%d with ' 'interval %rs', self.protocol, self.host, self.port, self.send_interval) async def stop(self, *_): self._handle.stop()
async def test_long_func(event_loop): counter = 0 async def task(): nonlocal counter counter += 1 await asyncio.sleep(0.5) periodic = PeriodicCallback(task) periodic.start(0.1, event_loop) await asyncio.sleep(1, loop=event_loop) periodic.stop() assert 1 < counter < 3
async def test_periodic(event_loop): counter = 0 def task(): nonlocal counter counter += 1 periodic = PeriodicCallback(task) periodic.start(0.1, event_loop) await asyncio.sleep(0.5, loop=event_loop) periodic.stop() assert 4 < counter < 7 await asyncio.sleep(0.5, loop=event_loop) assert 4 < counter < 7