Skip to content

Commit

Permalink
[Python] Ensure producer is keeping the client object alive (#11887)
Browse files Browse the repository at this point in the history
### 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 235e678)
  • Loading branch information
merlimat authored and hangc0276 committed Sep 4, 2021
1 parent 13416ea commit 09a6b58
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions pulsar-client-cpp/python/pulsar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ def create_producer(self, topic,
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,
Expand Down
8 changes: 8 additions & 0 deletions pulsar-client-cpp/python/pulsar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,14 @@ def test_producer_consumer_zstd(self):
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):
Expand Down

0 comments on commit 09a6b58

Please sign in to comment.