GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add futures for subscriptions. by lukesneeringer · Pull Request #4265 · googleapis/google-cloud-python

Conversation

@lukesneeringer

@lukesneeringer lukesneeringer commented Oct 26, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This causes the .open method on subscriptions to return a future, which can be used to block the main thread or trap exceptions.

Fixes #3888.
Fixes #4019.

@jonparrott A couple items we should nail down and possibly tweak before merging this:

  • The current interface has a three-object pass to start consuming messages: instantiate a client, instantiate a subscription, then call .open on the subscription, which now returns a future. However, we currently have some shortcut logic that lets you pass a callback on step 2. You still get the subscription back, but .open is called implicitly; there's no good place for me to return the future in that case.
    • This is kind of a big deal; this future is basically the way to both block and handle errata.
    • On the other hand, maybe this is fine; .future is a public property.
      • At the very minimum, I should update all documentation to show the full baton pass and highlight the future and what it is for.
  • There is not really any good way to stop consuming automatically. I can not do it in on_exception because that runs in one of the threads that the helper_threads.stop_all() method is trying to burn down, so it raises RuntimeError.
    • I could have an .add_done_callback call in Future.__init__ that adds a callback that does the right thing, but this feels like an interface violation to me.
    • Maybe this is fine too; we (a) leave it up to users to decide how to handle errors, albeit (b) also fix bugs like Pub/Sub Subscriber does not catch & retry UNAVAILABLE errors #4234 so they are far less common.

Holding off on updating tests and docs until we make a decision.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Luke Sneeringer
Add futures for subscriptions.
978775b
This causes the `.open` method on subscriptions to return a future,
which can be used to block the main thread or trap exceptions.
@lukesneeringer
lukesneeringer requested a review from theacodes October 26, 2017 18:19
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 26, 2017
@theacodes

theacodes commented Oct 26, 2017

Copy link
Copy Markdown
Contributor

On the other hand, maybe this is fine; .future is a public property.

Seems fine:

    subscriber = pubsub_v1.SubscriberClient()
    subscription_path = subscriber.subscription_path(
        project, subscription_name)

    def callback(message):
        print('Received message: {}'.format(message))
        message.ack()

    subcription = subscriber.subscribe(subscription_path, callback=callback)

    # The subscriber is non-blocking, so we must keep the main thread from
    # exiting to allow it to process messages in the background.
    print('Listening for messages on {}'.format(subscription_path))
    subscription.future.result()

There is not really any good way to stop consuming automatically.

doesn't .close() cover this? Or am I missing something?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lukesneeringer

lukesneeringer commented Oct 26, 2017

Copy link
Copy Markdown
Contributor Author

doesn't .close() cover this? Or am I missing something?

It does, but if you call it in .on_exception, you get a RuntimeError because a thread tries to join itself.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dhermes
dhermes reviewed Oct 26, 2017
View reviewed changes
Comment thread pubsub/google/cloud/pubsub_v1/publisher/batch/thread.py Outdated

# These objects are all communicated between threads; ensure that
# any writes to them are atomic.
# import pdb ; pdb.set_trace()

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@theacodes

theacodes commented Oct 26, 2017

Copy link
Copy Markdown
Contributor

Can we have close() send a signal to exit if it's being invoked from the "wrong" thread?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lukesneeringer

lukesneeringer commented Oct 26, 2017

Copy link
Copy Markdown
Contributor Author

Maybe.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Luke Sneeringer added 2 commits October 26, 2017 12:58
Luke Sneeringer
Re-factor futures into a hierarchy; add tests.
823c6a3
Luke Sneeringer
Document the futures coming from subscriber.
c358ff6
@lukesneeringer

lukesneeringer commented Oct 26, 2017

Copy link
Copy Markdown
Contributor Author

@jonparrott This is ready for review. I am going to punt on .close() for now because I think it is more valuable to push this as is and spend cycles on a couple of the other bugs. I am also not 100% sure that .close() is even the right behavior.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@theacodes

theacodes commented Oct 26, 2017

Copy link
Copy Markdown
Contributor

@lukesneeringer totally fair

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

theacodes
theacodes approved these changes Oct 26, 2017
View reviewed changes

@theacodes theacodes left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly fine, but some concerns about making the future public.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pubsub/google/cloud/pubsub_v1/futures.py Outdated
return True

def done(self):
"""Return True if the publish has completed, False otherwise.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pubsub/google/cloud/pubsub_v1/futures.py Outdated
return self._result
raise err

def exception(self, timeout=None, _wait=1):

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pubsub/google/cloud/pubsub_v1/futures.py Outdated
when the future finishes running.
"""
if self.done():
fn(self)

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pubsub/google/cloud/pubsub_v1/futures.py Outdated
"""Set the result of the future to the provided result.

Args:
result (str): The message ID.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pubsub/google/cloud/pubsub_v1/futures.py
@@ -0,0 +1,176 @@
# Copyright 2017, Google Inc. All rights reserved.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pubsub/google/cloud/pubsub_v1/publisher/futures.py
# The publishing-side subclass does not need any special behavior
# at this time.
#
# However, there is still a subclass so that if someone attempts

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Luke Sneeringer added 3 commits October 26, 2017 14:24
Luke Sneeringer
Address incorrect docs.
4e469aa
Luke Sneeringer
Remove _wait
9112aa7
Luke Sneeringer
Futures are canonically google.api_core.futures.Future.
03076f8
@lukesneeringer
lukesneeringer merged commit f383b8c into googleapis:master Oct 26, 2017
@lukesneeringer
lukesneeringer deleted the subscribe-future branch October 26, 2017 22:01
This was referenced Oct 26, 2017
Set gRPC message options and keepalive. #4269
Merged
Pub/Sub Subscriber does not catch & retry UNAVAILABLE errors #4234
Closed
parthea pushed a commit that referenced this pull request Mar 2, 2026
@lukesneeringer
Add futures for subscriptions. (#4265)
8cd03d7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@dhermes dhermes dhermes left review comments
@theacodes theacodes theacodes approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Publish future blocks indefinitely if API raises exception Pub/Sub has no way to track errors from the subscriber thread.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@lukesneeringer @theacodes @dhermes @googlebot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.