powerful. fast. easy.
The AMPS Client for Python combines performance, flexibility and ease-of-use in just the right way to make your messaging applications shine. Combine Python's productivity and expresiveness with the blazing performance of AMPS to create applications that win. The Python client is built on our C++ client, for the highest levels of performance and capability. Here are a few examples to get you started:
Example 1: Connect and Subscribe
| from AMPS import *
amps = Client("my-application")
try:
amps.connect("tcp://localhost:9007/amps/json")
amps.logon()
for message in amps.subscribe("orders"):
print (message.get_data())
except AMPSException, e:
print (e)
|
In this example, we connect to an AMPS server running locally and initiate a subscription to the "orders" topic. As new orders are posted, myListener is invoked with each message, and prints the data of each order to the console.
Example 2: Publish a Message
| amps = Client("publish-example")
amps.connect("tcp://127.0.0.1:9007/amps/xml")
amps.logon()
amps.publish("messages", "<hi>Hello, world!</hi>")
|
With AMPS, publishing is simple, as shown in this example. We connect to an AMPS server running locally, and publish a single message to the messages
topic. To simply publish a message, there is no need to predeclare the topic or configure complex routing. Any subscription that has asked for XML messages on the messages
topic will receive the message.
Example 3: Automatic Reconnection and Resubscription
Rock-solid applications must be able to recover from network outages. The AMPS Python client includes an HAClient
class that includes automatic reconnection and resubscription. Best of all, easy-to-implement interfaces control reconnection and resubscription behavior, allowing you to easily customize failover.
| client = AMPS.HAClient("reconnectingSubscriber")
# The ServerChooser interface tells the HAClient which server
# to connect to, both for the initial connection and failover.
# The DefaultServerChooser is included with the client: many
# applications implement a ServerChooser to control failover
# behavior.
sc = AMPS.DefaultServerChooser()
sc.add("tcp://amps-server:9007/amps/json")
sc.add("tcp://amps-failover-server:9007/amps/json")
client.set_server_chooser(sc)
client.connect_and_logon()
# Enter subscriptions. If the connection to the server
# is lost, the HAClient will restore the connection to
# the server or the failover partner, and restore
# subscriptions.
client.subscribe(handleMessage, "my-cool-topic")
|
The HAClient
class can, optionally, also provide store-and-forward for reliable publish. With the AMPS transaction log, the class can provide resumable subscriptions that are guaranteed not to miss messages or receive duplicate messages, even in the case of failover between replicated servers.
Example 4: Query the Contents of a "SOW" Topic
State-of-the-World ("SOW") topics in AMPS combine the power of a database table with the performance of a publish-subscribe system. Use the AMPS Python client to query the contents of a SOW topic.
| def execute_sow_query(client):
# runs the SOW query, processes all of the messages
# from the query, and then returns.
for message in client.sow("orders",
"/symbol='ROL'",
batch_size=100,
timeout=5000):
if message.get_command() == Message.Command.SOW:
print (message.get_data())
|
This example queries for all orders for the symbol ROL
, and simply prints the messages to the console.
Example 5: Automatic Failover
The AMPS Python client includes both a basic client, and a high availability client with additional features, including the ability to automatically failover if the client is disconnected. This example shows how to set up a high availability client for failover:
| client = AMPS.HAClient("myClient")
# primary.amps.xyz.com is the primary AMPS instance, and
# secondary.amps.xyz.com is the secondary
# create a server chooser
chooser = AMPS.DefaultServerChooser()
# add the addresses to use for failover
chooser.add("tcp://primary.amps.xyz.com:12345/amps/json")
chooser.add("tcp://secondary.amps.xyz.com:12345/amps/json")
# set the server chooser for the client
client.set_server_chooser(chooser)
# connect and logon
client.connect_and_logon()
# now, use the client: if the client detects
# a disconnection, it will reconnect.
...
# at the end of the program, disconnect
client.disconnect()
|
This example creates an HA client and a server chooser for the client. The code then populates the server chooser with the list of failover servers, adds the chooser to the client, and then connects.
Once the client is connected, you can use the HAClient object just like a regular AMPS client. You can take advantage of the extended features, such as durable publish, duplicate message protection, and so on -- see the Developer's Guide for more information!
Everything You Need
Ready to learn more? The AMPS Client for Python includes everything you need to get started: libraries, sample applications, developer's guide, and reference documentation. Each AMPS Client includes complete source code so you can fully learn and understand AMPS, and best integrate it into your environment.
Click here to download and start using the AMPS Client for Python. Once you've downloaded it, read the Developer Guide to get started.
If you need more help getting started, the 60East Technologies support and services team is ready to provide the support you need to help you CRANK UP THE AMPS.