You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Callback mechanism for listening to Pub/Sub messages #1791
I'd love for there to be a mechanism to set up perpetual pulling and acknowledging of Cloud Pub/Sub messages, the sort of thing that might enable code like this:
# Messages are acknowledged unless DoWork raises an exception
def DoWork(message):
print "Message payload: %s" % message.payload
worker_thread = subscription.pull(callback=DoWork)
This would be pretty interesting, but there's lots of questions there on how that happens. Is it a thread? another process? gevent? asyncio?
In the meantime, GoogleCloudPlatform/psq is a rather high-level layer over Pub/Sub that can handle a similar use case: polling for messages and executing functions.
psq is pretty cool, but I believe it's a bit specialized into the niche of low configuration task queue. For example, I was under the impression that it only worked for topics that began with the name "psq-" and assumed the existence of a shared subscription on that queue.
Supporting callbacks on some arbitrary subscription would either be a feature request on gcloud-pubsub for callbacks or a feature request on psq for supporting a single, arbitrary subscription. I could file the latter, if you'd prefer not to add callbacks on this side, but I think I'd prefer it be added to gcloud-pubsub. It seems like a thread that continually polled for messages and dispatched them to a handler would be pretty much universally useful to gcloud-pubsub users.
Callbacks in Python are usually reserved for asynchronous programming. It seems in your FR that you expected calling subscription.pull(callback=DoWork) to not block. Unfortunately, that gets into the realm of async and there there are a multitude of ways to do that in Python and it would seem unfair for us to choose one way and limit our users.
For example, if someone wanted to use gevent to do what you said, they could easily implement that via:
Activity
theacodes commentedon May 11, 2016
This would be pretty interesting, but there's lots of questions there on how that happens. Is it a thread? another process? gevent? asyncio?
In the meantime, GoogleCloudPlatform/psq is a rather high-level layer over Pub/Sub that can handle a similar use case: polling for messages and executing functions.
BrandonY commentedon May 12, 2016
psq is pretty cool, but I believe it's a bit specialized into the niche of low configuration task queue. For example, I was under the impression that it only worked for topics that began with the name "psq-" and assumed the existence of a shared subscription on that queue.
Supporting callbacks on some arbitrary subscription would either be a feature request on gcloud-pubsub for callbacks or a feature request on psq for supporting a single, arbitrary subscription. I could file the latter, if you'd prefer not to add callbacks on this side, but I think I'd prefer it be added to gcloud-pubsub. It seems like a thread that continually polled for messages and dispatched them to a handler would be pretty much universally useful to gcloud-pubsub users.
theacodes commentedon May 12, 2016
So, Pythonically I don't need much benefit over:
vs
Callbacks in Python are usually reserved for asynchronous programming. It seems in your FR that you expected calling
subscription.pull(callback=DoWork)to not block. Unfortunately, that gets into the realm of async and there there are a multitude of ways to do that in Python and it would seem unfair for us to choose one way and limit our users.For example, if someone wanted to use gevent to do what you said, they could easily implement that via:
@dhermes or @tseaver might have some different ideas here.
BrandonY commentedon May 12, 2016
When you add in the need to acknowledge messages, I think the below is pretty much the simplest way to use the pull function:
That's still pretty easy to write, but it's also pretty easy to get wrong, so I still like the idea of gcloud-pubsub offering it directly.
tseaver commentedon May 12, 2016
FWIW, PR's #1636 and #1637 offer competing strategies for doing "auto-acknowlegement" of message IDs.
BrandonY commentedon May 12, 2016
Ah, very interesting, thanks @tseaver
tseaver commentedon Aug 25, 2016
And FTR we merged the implementation in #1636, making your example above look like:
more info on alternatives [(#1791)](GoogleCloudPlatform/python-docs-s…
feat: Implement ST_LENGTH geography function (#1791)