-
Observed behaviorHi, I may be doing something wrong but I couldn't find a good way to do this. If I try to reuse a JS context, it fails to send any messages and times out on every send Apologies if I am trying to solve this the wrong way, I have been through all the docs, asked on the slack and gone over most of the source code. Expected behaviorThe context should be capable of sending messages later. Ideally, without reconnecting as the connect takes around 30ms. Server and client versionnats-py v2.10.0 Host environmentNo response Steps to reproduceNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
So the issue is likely, that you can't reuse a NATS connection across different event loops. When you do something like multiple |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, thats the case. There's also nothing in the library that generates a thread or an event loop I can see, so you have to do that yourself I think. The tests run with asyncio.run() which creates a loop and then closes it after the async calls have finished. If other people want to get it running, I think you want to do something like this: class JSSyncToAsync:
nats_servers: list[str]
loop: AbstractEventLoop
def set_loop_and_connect(self: Self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.added_topics = set()
Thread(target=self.run_client, daemon=True).start()
asyncio.run_coroutine_threadsafe(self.connect(), self.loop)
def run_client(self: Self):
self.loop.run_forever()
async def connect(self: Self):
...This will create a thread and run the event loop on it forever, which will handle the async calls from the nats library. |
Beta Was this translation helpful? Give feedback.
Yeah, thats the case. There's also nothing in the library that generates a thread or an event loop I can see, so you have to do that yourself I think. The tests run with asyncio.run() which creates a loop and then closes it after the async calls have finished.
If other people want to get it running, I think you want to do something like this: