Maint/asyncio (#305)

* Move to asyncio services, even though everything is largely sync
This commit is contained in:
cybermaggedon 2025-02-11 23:24:46 +00:00 committed by GitHub
parent a0bf2362f6
commit f350abb415
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
60 changed files with 243 additions and 227 deletions

View file

@ -1,4 +1,5 @@
import asyncio
import os
import argparse
import pulsar
@ -83,11 +84,20 @@ class BaseProcessor:
help=f'Pulsar host (default: 8000)',
)
def run(self):
async def start(self):
pass
async def run(self):
raise RuntimeError("Something should have implemented the run method")
@classmethod
def start(cls, prog, doc):
async def launch_async(cls, args):
p = cls(**args)
await p.start()
await p.run()
@classmethod
def launch(cls, prog, doc):
parser = argparse.ArgumentParser(
prog=prog,
@ -108,8 +118,7 @@ class BaseProcessor:
try:
p = cls(**args)
p.run()
asyncio.run(cls.launch_async(args))
except KeyboardInterrupt:
print("Keyboard interrupt.")
@ -127,3 +136,4 @@ class BaseProcessor:
print("Will retry...", flush=True)
time.sleep(4)

View file

@ -1,4 +1,5 @@
import asyncio
from pulsar.schema import JsonSchema
import pulsar
from prometheus_client import Histogram, Info, Counter, Enum
@ -75,7 +76,7 @@ class Consumer(BaseProcessor):
print("Initialised consumer.", flush=True)
def run(self):
async def run(self):
__class__.state_metric.state('running')
@ -104,7 +105,7 @@ class Consumer(BaseProcessor):
try:
with __class__.request_metric.time():
self.handle(msg)
await self.handle(msg)
# Acknowledge successful processing of the message
self.consumer.acknowledge(msg)

View file

@ -42,7 +42,7 @@ class ConsumerProducer(Consumer):
print("Initialised consumer/producer.")
def send(self, msg, properties={}):
async def send(self, msg, properties={}):
self.producer.send(msg, properties)
__class__.output_metric.inc()

View file

@ -37,7 +37,7 @@ class Producer(BaseProcessor):
chunking_enabled=True,
)
def send(self, msg, properties={}):
async def send(self, msg, properties={}):
self.producer.send(msg, properties)
__class__.output_metric.inc()