I have a class in a third-party package that maintains a socket-level connection, and provides a method to send via the socket, and a receive loop method (intended to be run as a thread target) that receives and forms messages off that socket, and a callback loop method that then calls an arbitrary callable passing along that formed message (and is also intended to be run as a thread target).
I’ve incorporated an instance of this class into a DependencyProvider and now my goal is to publish events whenever the class callback method fires.
My question is how to accomplish this?
import socketthing from nameko import config from nameko.extensions import DependencyProvider from nameko.rpc import rpc class ConnectionWrapper: def __init__(self, client): self.client = client def get_info(self): return self.client.get_connection_info() class Connection(DependencyProvider): def setup(self): self.client = socketthing.SocketThing(spawn_threads=False) self.client.callback = self.handler self.client.connect(config.get("SOME_ADDRESS")) self.container.spawn_managed_thread( self.client._receive_loop, identifier="Receive loop" ) self.container.spawn_managed_thread( self.client._callback_loop, identifier="Callback loop" ) def get_dependency(self, worker_ctx): return ConnectionWrapper(self.client) def handler(self, payload): # Publish an event here instead! print(payload) class ConnectionService: name = 'connection' connection = Connection() @rpc def get_info(self): return self.connection.get_info()