Asynchronous Message Processing
Last updated
Last updated
The AMPS Python client also supports an interface that allows you to process messages asynchronously. In this case, you add a message handler to the method call. The client object returns the command ID of the subscribe command once the server has acknowledged that the command has been processed. As messages arrive, the client calls your message handler directly on the background thread. This can be an advantage for some applications. For example, if your application is highly multithreaded and copies message data to a work queue processed by multiple threads, there may be a performance benefit to enqueuing work directly from the background thread. See for a discussion of threading considerations, including considerations for message handlers.
As with the simple, synchronous interface, the AMPS client provides both convenience methods and methods that use a Command
object. The following example shows how to use the asynchronous message processing interface (error handling and connection details are omitted for brevity):
One of the more common ways of providing a message handler is as an instance method on an object that maintains message state. It's simple to provide a handler with this capability, as shown below:
You can then provide an instance of the handler directly wherever a message handler is required, as shown below: