pythonmultithreadingpython-asynciogoogle-cloud-pubsub

Using subscribe() method in an asyncio stack


I want to use pubsub in an application that uses asyncio as a basic way of achieving I/O concurrency. The default Google SDK, however doesn't offer async methods (I have tried gcloud-aio-pubsub, but it only supports pull methods, so the latency is very bad).

With publishing and topic/subscription management I could just use wrap_future, as the publish() method and most other return an object supporting Future protocol.

My question is about the subscribe method. According to the documentation it runs the StreamingPull in a separate thread. Also, if I understand the code correctly, I see that the callback is called in a threat that is taken from a pool.

My idea to use it with asyncio is to replace the default Scheduler with one that would schedule the coroutines in the main thread's event loop. Is it a good approach? Any considerations that I need to take into account?


Solution

  • Yes that would do as it can avoid thread conflicts, but make sure to check on what could block the event loop, callback might block the asyncio event loop. Unhandled exceptions in callback might also terminate your subscription. Also, be cautious about sharing state between callback and other parts of your application for thread safety.