You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ch...@apache.org on 2021/09/04 00:19:17 UTC

[pulsar] 05/05: [Python] Ensure producer is keeping the client object alive (#11887)

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

chenhang pushed a commit to branch branch-2.8
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 09a6b588fe7df63c6cd8acb6699cab46f8f57bec
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Wed Sep 1 19:31:18 2021 -0700

    [Python] Ensure producer is keeping the client object alive (#11887)
    
    ### Motivation
    
    Fix #6463.
    
    When a producer is created, keep a reference on the client object so that the Python GC will not destroy it. This is similar to what we were already doing for consumer and reader, were we had examples with listeners that need to keep the client/consumer alive.
    
    (cherry picked from commit 235e678a56d0284e68b45e46706b6237d7c6d5f9)
---
 pulsar-client-cpp/python/pulsar/__init__.py | 1 +
 pulsar-client-cpp/python/pulsar_test.py     | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/pulsar-client-cpp/python/pulsar/__init__.py b/pulsar-client-cpp/python/pulsar/__init__.py
index 514ca11..429aa10 100644
--- a/pulsar-client-cpp/python/pulsar/__init__.py
+++ b/pulsar-client-cpp/python/pulsar/__init__.py
@@ -588,6 +588,7 @@ class Client:
         p = Producer()
         p._producer = self._client.create_producer(topic, conf)
         p._schema = schema
+        p._client = self._client
         return p
 
     def subscribe(self, topic, subscription_name,
diff --git a/pulsar-client-cpp/python/pulsar_test.py b/pulsar-client-cpp/python/pulsar_test.py
index 5810745..4a8ca03 100755
--- a/pulsar-client-cpp/python/pulsar_test.py
+++ b/pulsar-client-cpp/python/pulsar_test.py
@@ -1096,6 +1096,14 @@ class PulsarTest(TestCase):
         consumer.unsubscribe()
         client.close()
 
+    def test_client_reference_deleted(self):
+        def get_producer():
+            cl = Client(self.serviceUrl)
+            return cl.create_producer(topic='foobar')
+
+        producer = get_producer()
+        producer.send(b'test_payload')
+
     #####
 
     def test_get_topic_name(self):