You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/06/16 01:00:44 UTC

[pulsar] branch master updated: [Doc]--[python client] negative acks (#7263)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 71ebb53  [Doc]--[python client] negative acks (#7263)
71ebb53 is described below

commit 71ebb53c31517b512269cfb307030150efa4e9e7
Author: HuanliMeng <48...@users.noreply.github.com>
AuthorDate: Tue Jun 16 09:00:30 2020 +0800

    [Doc]--[python client] negative acks (#7263)
    
    ### Motivation
    
    In Pulsar 2.4.0, negative acknowledgement is supported in Python client. But the doc has no such code example.
    
    So this PR is for adding negative ack code example for releases: master--2.4.0.
    
    The doc update is approved in PR: https://github.com/apache/pulsar/pull/7148. But the PR fails CI test. So i close that PR and re-create a new PR.
    
    
    
    ### Modifications
    
    Add negative ack code example to python client doc for releases master--2.4.0.
---
 site2/docs/client-libraries-python.md              | 27 +++++++++++
 .../client-libraries-python.md                     | 54 ++++++++++++++++++----
 .../client-libraries-python.md                     | 40 ++++++++++++++--
 .../client-libraries-python.md                     | 40 ++++++++++++++--
 .../version-2.5.0/client-libraries-python.md       | 27 +++++++++++
 .../version-2.5.1/client-libraries-python.md       | 27 +++++++++++
 .../version-2.5.2/client-libraries-python.md       | 27 +++++++++++
 7 files changed, 226 insertions(+), 16 deletions(-)

diff --git a/site2/docs/client-libraries-python.md b/site2/docs/client-libraries-python.md
index 4d5c0b0..3b2c60a 100644
--- a/site2/docs/client-libraries-python.md
+++ b/site2/docs/client-libraries-python.md
@@ -86,6 +86,33 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+client = Client('pulsar://localhost:6650')
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+producer.flush()
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example:
diff --git a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md b/site2/website/versioned_docs/version-2.4.0/client-libraries-python.md
similarity index 79%
copy from site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
copy to site2/website/versioned_docs/version-2.4.0/client-libraries-python.md
index c9f3925..c20e9f6 100644
--- a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
+++ b/site2/website/versioned_docs/version-2.4.0/client-libraries-python.md
@@ -1,5 +1,5 @@
 ---
-id: version-2.5.0-client-libraries-python
+id: version-2.4.0-client-libraries-python
 title: The Pulsar Python client
 sidebar_label: Python
 original_id: client-libraries-python
@@ -23,12 +23,12 @@ Installation via PyPi is available for the following Python versions:
 
 Platform | Supported Python versions
 :--------|:-------------------------
-MacOS <br />  10.13 (High Sierra), 10.14 (Mojave) <br /> | 2.7, 3.7
-Linux | 2.7, 3.4, 3.5, 3.6, 3.7
+| MacOS<br /> 10.11 (El Capitan) — 10.12 (Sierra) — 10.13 (High Sierra) — 10.14 (Mojave)<br />| 2.7, 3.7
+|Linux | 2.7, 3.4, 3.5, 3.6, 3.7
 
 ### Installing from source
 
-To install the `pulsar-client` library by building from source, follow [these instructions](client-libraries-cpp.md#compilation) and compile the Pulsar C++ client library. That will also build the Python binding for the library.
+To install the `pulsar-client` library by building from source, follow [these instructions](client-libraries-cpp.md#compilation) and compile the Pulsar C++ client library. That also builds the Python binding for the library.
 
 To install the built Python bindings:
 
@@ -44,7 +44,7 @@ The complete Python API reference is available at [api/python](/api/python).
 
 ## Examples
 
-Below you'll find a variety of Python code examples for the `pulsar-client` library.
+You can find a variety of Python code examples for the `pulsar-client` library as below.
 
 ### Producer example
 
@@ -65,7 +65,7 @@ client.close()
 
 ### Consumer example
 
-This creates a consumer with the `my-subscription` subscription on the `my-topic` topic, listen for incoming messages, print the content and ID of messages that arrive, and acknowledge each message to the Pulsar broker:
+This example creates a consumer with the `my-subscription` subscription on the `my-topic` topic, listens for incoming messages, prints the content and ID of messages that arrive, and acknowledges each message to the Pulsar broker:
 
 ```python
 consumer = client.subscribe('my-topic', 'my-subscription')
@@ -83,6 +83,40 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+
+client = Client('pulsar://localhost:6650')
+
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+
+producer.flush()
+
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example:
@@ -128,19 +162,19 @@ producer = client.create_producer(
 producer.send(Example(a='Hello', b=1))
 ```
 
-When the producer is created, the Pulsar broker will validate that
+When the producer is created, the Pulsar broker validates that
 the existing topic schema is indeed of "Avro" type and that the
 format is compatible with the schema definition of the `Example`
 class.
 
-If there is a mismatch, the producer creation will raise an
+If there is a mismatch, the producer creation raises an
 exception.
 
 Once a producer is created with a certain schema definition,
-it will only accept objects that are instances of the declared
+it only accepts objects that are instances of the declared
 schema class.
 
-Similarly, for a consumer/reader, the consumer will return an
+Similarly, for a consumer/reader, the consumer returns an
 object, instance of the schema record class, rather than the raw
 bytes:
 
diff --git a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md b/site2/website/versioned_docs/version-2.4.1/client-libraries-python.md
similarity index 87%
copy from site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
copy to site2/website/versioned_docs/version-2.4.1/client-libraries-python.md
index c9f3925..3bd3a8c 100644
--- a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
+++ b/site2/website/versioned_docs/version-2.4.1/client-libraries-python.md
@@ -1,5 +1,5 @@
 ---
-id: version-2.5.0-client-libraries-python
+id: version-2.4.1-client-libraries-python
 title: The Pulsar Python client
 sidebar_label: Python
 original_id: client-libraries-python
@@ -23,8 +23,8 @@ Installation via PyPi is available for the following Python versions:
 
 Platform | Supported Python versions
 :--------|:-------------------------
-MacOS <br />  10.13 (High Sierra), 10.14 (Mojave) <br /> | 2.7, 3.7
-Linux | 2.7, 3.4, 3.5, 3.6, 3.7
+| MacOS<br /> 10.11 (El Capitan) — 10.12 (Sierra) — 10.13 (High Sierra) — 10.14 (Mojave)<br />| 2.7, 3.7
+|Linux | 2.7, 3.4, 3.5, 3.6, 3.7
 
 ### Installing from source
 
@@ -83,6 +83,40 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+
+client = Client('pulsar://localhost:6650')
+
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+
+producer.flush()
+
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example:
diff --git a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md b/site2/website/versioned_docs/version-2.4.2/client-libraries-python.md
similarity index 87%
copy from site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
copy to site2/website/versioned_docs/version-2.4.2/client-libraries-python.md
index c9f3925..a0b74d5 100644
--- a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
+++ b/site2/website/versioned_docs/version-2.4.2/client-libraries-python.md
@@ -1,5 +1,5 @@
 ---
-id: version-2.5.0-client-libraries-python
+id: version-2.4.2-client-libraries-python
 title: The Pulsar Python client
 sidebar_label: Python
 original_id: client-libraries-python
@@ -23,8 +23,8 @@ Installation via PyPi is available for the following Python versions:
 
 Platform | Supported Python versions
 :--------|:-------------------------
-MacOS <br />  10.13 (High Sierra), 10.14 (Mojave) <br /> | 2.7, 3.7
-Linux | 2.7, 3.4, 3.5, 3.6, 3.7
+| MacOS<br /> 10.11 (El Capitan) — 10.12 (Sierra) — 10.13 (High Sierra) — 10.14 (Mojave)<br />| 2.7, 3.7
+|Linux | 2.7, 3.4, 3.5, 3.6, 3.7
 
 ### Installing from source
 
@@ -83,6 +83,40 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+
+client = Client('pulsar://localhost:6650')
+
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+
+producer.flush()
+
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example:
diff --git a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md b/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
index c9f3925..1df0ca0 100644
--- a/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
+++ b/site2/website/versioned_docs/version-2.5.0/client-libraries-python.md
@@ -83,6 +83,33 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+client = Client('pulsar://localhost:6650')
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+producer.flush()
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example:
diff --git a/site2/website/versioned_docs/version-2.5.1/client-libraries-python.md b/site2/website/versioned_docs/version-2.5.1/client-libraries-python.md
index ce67891..bef50e5 100644
--- a/site2/website/versioned_docs/version-2.5.1/client-libraries-python.md
+++ b/site2/website/versioned_docs/version-2.5.1/client-libraries-python.md
@@ -87,6 +87,33 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+client = Client('pulsar://localhost:6650')
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+producer.flush()
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example:
diff --git a/site2/website/versioned_docs/version-2.5.2/client-libraries-python.md b/site2/website/versioned_docs/version-2.5.2/client-libraries-python.md
index 7df84f9..11f5130 100644
--- a/site2/website/versioned_docs/version-2.5.2/client-libraries-python.md
+++ b/site2/website/versioned_docs/version-2.5.2/client-libraries-python.md
@@ -87,6 +87,33 @@ while True:
 client.close()
 ```
 
+This example shows how to configure negative acknowledgement.
+
+```python
+from pulsar import Client, schema
+client = Client('pulsar://localhost:6650')
+consumer = client.subscribe('negative_acks','test',schema=schema.StringSchema())
+producer = client.create_producer('negative_acks',schema=schema.StringSchema())
+for i in range(10):
+    print('send msg "hello-%d"' % i)
+    producer.send_async('hello-%d' % i, callback=None)
+producer.flush()
+for i in range(10):
+    msg = consumer.receive()
+    consumer.negative_acknowledge(msg)
+    print('receive and nack msg "%s"' % msg.data())
+for i in range(10):
+    msg = consumer.receive()
+    consumer.acknowledge(msg)
+    print('receive and ack msg "%s"' % msg.data())
+try:
+    # No more messages expected
+    msg = consumer.receive(100)
+except:
+    print("no more msg")
+    pass
+```
+
 ### Reader interface example
 
 You can use the Pulsar Python API to use the Pulsar [reader interface](concepts-clients.md#reader-interface). Here's an example: