mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-26 17:06:22 +02:00
Maint/asyncio (#305)
* Move to asyncio services, even though everything is largely sync
This commit is contained in:
parent
a0bf2362f6
commit
f350abb415
60 changed files with 243 additions and 227 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue