candlerb opened a new issue #5662: [doc] API documentation doesn't describe 
behaviour of async callbacks
URL: https://github.com/apache/pulsar/issues/5662
 
 
   **Is your feature request related to a problem? Please describe.**
   The various APIs include asynchronous operations with callbacks on 
completion - for example the [Python 
API](https://pulsar.apache.org/docs/en/client-libraries-python) has 
[send_async](https://pulsar.apache.org/api/python/#pulsar.Producer.send_async).
   
   However, I cannot see it documented anywhere whether the callbacks occur in 
a different thread (and hence could occur at any instant) or in the same thread 
(and won't occur until you next call some other pulsar API method)
   
   This has a big impact on how you use the API.  If it's in a different 
thread: then you have to protect all data structures that the callback may 
touch using mutexes.  If it's in the same thread: then you have to make sure 
your application calls an pulsar API method periodically to avoid starvation.
   
   I ended up having to test the behaviour.
   
   ```
   import pulsar
   import threading
   import time
   
   client = pulsar.Client('pulsar://localhost:6650')
   
   producer = client.create_producer('temp', producer_name='fred')
   
   def ack(*args, **kwargs):
       print("Got ack: %r %r in thread %r" % (args, kwargs, 
threading.get_ident()))
   
   print("Main thread %r" % threading.get_ident())
   producer.send_async(b"Hello 1", callback=ack)
   print("Sleeping...")
   time.sleep(1)
   producer.send_async(b"Hello 2", callback=ack)
   print("Sleeping...")
   time.sleep(1)
   
   producer.close()
   client.close()
   ```
   
   Result:
   
   ```
   Main thread 140330719487808
   Sleeping...
   Got ack: (_pulsar.Result.Ok, <_pulsar.Message object at 0x7fa14931f190>) {} 
in thread 140330630002432
   Sleeping...
   Got ack: (_pulsar.Result.Ok, <_pulsar.Message object at 0x7fa14931f190>) {} 
in thread 140330630002432
   ```
   
   This shows that acks are delivered in a different thread.  I think such 
important semantics should be documented.
   
   Aside: there *is* a test for send_async in 
[pulsar_test.py](https://github.com/apache/pulsar/blob/master/pulsar-client-cpp/python/pulsar_test.py#L104),
 which could only work if callbacks are invoked in a different thread.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to