Skip to content

Commit

Permalink
Updated Python examples to work with py-2 and py-3 (apache#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat authored and sijie committed Apr 29, 2018
1 parent 8eb2d21 commit 66fa03f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 42 deletions.
11 changes: 6 additions & 5 deletions pulsar-client-cpp/python/examples/rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import uuid


DEFAULT_CLIENT_TOPIC = 'persistent://sample/standalone/ns/rpc-client-topic'
DEFAULT_SERVER_TOPIC = 'persistent://sample/standalone/ns/rpc-server-topic'
DEFAULT_CLIENT_TOPIC = 'rpc-client-topic'
DEFAULT_SERVER_TOPIC = 'rpc-server-topic'
UUID = str(uuid.uuid4())
NUM_CLIENT = 0
LOCK = threading.Lock()
Expand Down Expand Up @@ -58,13 +58,14 @@ def __init__(self,
def on_response(self, consumer, message):
if message.partition_key() == self.partition_key \
and consumer.topic() == self.client_topic:
print('Received: {0}'.format(message.data()))
self.response = message.data().decode()
msg = message.data().decode('utf-8')
print('Received: {0}'.format(msg))
self.response = msg
consumer.acknowledge(message)

def call(self, message):
self.response = None
self.producer.send(message, partition_key=self.partition_key)
self.producer.send(message.encode('utf-8'), partition_key=self.partition_key)

while self.response is None:
pass
Expand Down
8 changes: 4 additions & 4 deletions pulsar-client-cpp/python/examples/rpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import pulsar


DEFAULT_CLIENT_TOPIC = 'persistent://sample/standalone/ns/rpc-client-topic'
DEFAULT_SERVER_TOPIC = 'persistent://sample/standalone/ns/rpc-server-topic'
DEFAULT_CLIENT_TOPIC = 'rpc-client-topic'
DEFAULT_SERVER_TOPIC = 'rpc-server-topic'


class RPCServer(object):
Expand All @@ -43,9 +43,9 @@ def __init__(self,

def on_response(self, consumer, message):
print('Received from {0}: {1}'.format(message.partition_key(),
message.data().decode()))
message.data().decode('utf-8')))

self.producer.send('{} bar'.format(message.data().decode()),
self.producer.send('{} bar'.format(message.data().decode('utf-8')),
partition_key=message.partition_key())
consumer.acknowledge(message)

Expand Down
19 changes: 8 additions & 11 deletions pulsar-client-cpp/python/pulsar.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@
client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer(
'persistent://sample/standalone/ns/my-topic')
producer = client.create_producer('my-topic')
for i in range(10):
producer.send('Hello-%d' % i)
producer.send(('Hello-%d' % i).encode('utf-8'))
client.close()
Expand All @@ -65,13 +64,11 @@
import pulsar
client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe(
'persistent://sample/standalone/ns/my-topic',
'my-sub')
consumer = client.subscribe('my-topic', 'my-subscription')
while True:
msg = consumer.receive()
print("Received message '%s' id='%s'", msg.data(), msg.message_id())
print("Received message '%s' id='%s'", msg.data().decode('utf-8'), msg.message_id())
consumer.acknowledge(msg)
client.close()
Expand All @@ -84,7 +81,7 @@
client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer(
'persistent://sample/standalone/ns/my-topic',
'my-topic',
block_if_queue_full=True,
batching_enabled=True,
batching_max_publish_delay_ms=10
Expand All @@ -94,7 +91,7 @@ def send_callback(res, msg):
print('Message published res=%s', res)
while True:
producer.send_async('Hello-%d' % i, send_callback)
producer.send_async(('Hello-%d' % i).encode('utf-8'), send_callback)
client.close()
"""
Expand Down Expand Up @@ -554,7 +551,7 @@ def send(self, content,
**Args**
* `content`:
A string with the message payload
A `bytes` object with the message payload.
**Options**
Expand Down Expand Up @@ -604,7 +601,7 @@ def callback(res, msg):
**Args**
* `content`:
A string with the message payload.
A `bytes` object with the message payload.
**Options**
Expand Down
4 changes: 2 additions & 2 deletions pulsar-client-cpp/python/test_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import pulsar

client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe('persistent://sample/standalone/ns/my-topic', "my-sub")
consumer = client.subscribe('my-topic', "my-subscription")

while True:
msg = consumer.receive()
print("Received message '{0}' id='{1}'".format(msg.data(), msg.message_id()))
print("Received message '{0}' id='{1}'".format(msg.data().decode('utf-8'), msg.message_id()))
consumer.acknowledge(msg)

client.close()
4 changes: 2 additions & 2 deletions pulsar-client-cpp/python/test_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
client = pulsar.Client('pulsar://localhost:6650')

producer = client.create_producer(
'persistent://sample/standalone/ns/my-topic',
'my-topic',
block_if_queue_full=True,
batching_enabled=True,
batching_max_publish_delay_ms=10
)

for i in range(10):
try:
producer.send('hello', None)
producer.send('hello'.encode('utf-8'), None)
except Exception as e:
print("Failed to send message: %s", e)

Expand Down
21 changes: 8 additions & 13 deletions site/docs/latest/clients/Python.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,27 @@ Below you'll find a variety of Python code examples for the `pulsar-client` libr

### Producer example

This creates a Python {% popover producer %} for the `persistent://sample/standalone/ns/my-topic` topic and send 10 messages on that topic:
This creates a Python {% popover producer %} for the `my-topic` topic and send 10 messages on that topic:

```python
import pulsar

TOPIC = 'persistent://sample/standalone/ns/my-topic'
PULSAR_SERVICE_URL = 'pulsar://localhost:6650'
client = pulsar.Client('pulsar://localhost:6650')

client = pulsar.Client(PULSAR_SERVICE_URL)

producer = client.create_producer(TOPIC)
producer = client.create_producer('my-topic')

for i in range(10):
producer.send('Hello-%d' % i)
producer.send(('Hello-%d' % i).encode('utf-8'))

client.close()
```

### Consumer example

This creates a {% popover consumer %} with the `my-sub` {% popover subscription %} on the `persistent://sample/standalone/ns/my-topic` topic, listen for incoming messages, print the content and ID of messages that arrive, and {% popover acknowledge %} each message to the Pulsar {% popover broker %}:
This creates a {% popover consumer %} with the `my-subscription` {% popover subscription %} on the `my-topic` topic, listen for incoming messages, print the content and ID of messages that arrive, and {% popover acknowledge %} each message to the Pulsar {% popover broker %}:

```python
SUBSCRIPTION = 'my-sub'

consumer = client.subscribe(TOPIC, SUBSCRIPTION)
consumer = client.subscribe('my-topic', 'my-subscription')

while True:
msg = consumer.receive()
Expand All @@ -112,10 +107,10 @@ You can use the Pulsar Python API to use the Pulsar [reader interface](../../get
# MessageId taken from a previously fetched message
msg_id = msg.message_id()

reader = client.create_reader(TOPIC, msg_id)
reader = client.create_reader('my-topic', msg_id)

while True:
msg = reader.receive()
print("Received message '%s' id='%s'", msg.data(), msg.message_id())
# No acknowledgment
```
```
10 changes: 5 additions & 5 deletions site/docs/latest/getting-started/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ docker run -it \
-p 8080:8080 \
-v $PWD/data:/pulsar/data \
apachepulsar/pulsar:{{site.current_version}} \
bin/pulsar standalone --advertised-address 127.0.0.1
bin/pulsar standalone
```

A few things to note about this command:
Expand Down Expand Up @@ -92,7 +92,7 @@ First create a consumer and subscribe to the topic:
import pulsar

client = pulsar.Client('pulsar://localhost:6650')
consumer = client.subscribe('persistent://sample/standalone/ns1/my-topic',
consumer = client.subscribe('my-topic',
subscription_name='my-sub')

while True:
Expand All @@ -109,10 +109,10 @@ Now we can start a producer to send some test messages:
import pulsar

client = pulsar.Client('pulsar://localhost:6650')
producer = client.create_producer('persistent://sample/standalone/ns1/my-topic')
producer = client.create_producer('my-topic')

for i in range(10):
producer.send('hello-pulsar-%d' % i)
producer.send(('hello-pulsar-%d' % i).encode('utf-8'))

client.close()
```
Expand All @@ -126,7 +126,7 @@ You can find detailed documentation of all the APIs in the [Admin API Overview](
In the simplest example, you can use curl to probe the stats for a particular topic:

```shell
$ curl http://localhost:8080/admin/persistent/sample/standalone/ns1/my-topic/stats | python -m json.tool
$ curl http://localhost:8080/admin/persistent/public/default/my-topic/stats | python -m json.tool
```

The output will be something like this:
Expand Down

0 comments on commit 66fa03f

Please sign in to comment.