Replies: 2 comments 3 replies
|
What I've come up with doesn't seem optimal, but it works by hiding away multiple blocking synchronous code blocks into threads and then putting the messages received into an janus.Queue (asyncio.Queues are not thread safe, so we swapped to janus). This all works under FastAPI to support the existing REST endpoints we serve. class Consumer(Thread):
def __init__(self, queue: janus.Queue, topic: str, schema: pulsar.schema.AvroSchema):
super().__init__()
self.queue = queue
self.pulsar_client = pulsar.Client("pulsar://pulsar:6650")
self.topic = topic
self.schema = schema
def run(self):
reader = self.pulsar_client.create_reader(
self.topic, start_message_id=pulsar.MessageId.latest, schema=self.schema
)
while True:
msg = reader.read_next()
topic = msg.topic_name()
if "/" in topic:
topic = topic.split("/")[-1]
message = from_avro(msg.value())
self.queue.sync_q.put_nowait(message)Then when the FastAPI app starts you can kick off the threads, and also launch an app = FastAPI()
app.queue = asyncio.Queue()
@app.on_event("startup")
async def startup_event():
consumer_preds = Consumer(app.queue, "predictions", Prediction.schema())
consumer_preds.start()
consumer_trades = Consumer(app.queue, "trades", Trade.schema())
consumer_trades.start()
asyncio.create_task(receive_message())
|
0 replies
|
If pydantic, how about using |
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi all, I'm trying to write up a simple proof-of-concept application, wherein all it does it subscribe to two separate topics, each with their own different AvroSchema. So far, this doesn't seem possible to do easily with the (synchronous) python client, because each
Consumeronly supports one type of schema, and aconsumer.receive()blocks.Does anyone know a nice solution for this?
All reactions