Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make publish futures compatible with concurrent.futures.as_completed() #374

Closed
wants to merge 3 commits into from

Conversation

plamut
Copy link
Contributor

@plamut plamut commented Apr 13, 2021

Closes #368.

After studying the as_completed() code from the standard library, it actually seemed easier to modify our futures implementation, as opposed to writing our own as_completed() logic, which would still require changes to the Future, too (think: locking and notifying).

The drawback of course is relying on implementation details from concurrent.futures, but good tests should help us here. Let's discuss this drawback here.

Current approach pros:

  • Fewer code changes, no need to write our own as_completed() logic.
  • The library's API surface does not change, no incompatibility with other official Pub/Sub clients, no dilemma and coordination on whether to move the implementation to api-core.

Current approach cons:

  • Compatibility depends on concurrent.futures implementation details, although tests should help here. In addition, the standard lib's implementation seems very stable, and changes infrequent (blame) - the key implementation details are more than 10 years old.

PR checklist:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

@product-auto-label product-auto-label bot added the api: pubsub Issues related to the googleapis/python-pubsub API. label Apr 13, 2021
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Apr 13, 2021
@plamut plamut requested a review from pradn April 13, 2021 17:51
@plamut
Copy link
Contributor Author

plamut commented Apr 13, 2021

@pradn PTAL, if you have time.

It's true that the solution uses concurrent.futures implementation details, but the required changes are relatively small and we'll have tests to protect us.

Alternatively, we could provide our own as_completed helper, but that would likely require a bit more code, including the changes to our Future itself, plus we would be adding new stuff to the library surface that other clients probably don't have.

After reading the standard library implementation, the first approach actually seemed more favourable (IMHO).

@pradn
Copy link
Contributor

pradn commented Apr 13, 2021

I can take a look later this week or early next week. :)

The futures implementation is adjusted to work well with the built-in
function with the same name in `concurrent.futures` package.
The overriden method is exactly the same as the base method, only
it's docstring is different - no need to keep the code when super()
achieves the same.
@plamut plamut marked this pull request as ready for review April 14, 2021 10:16
@plamut plamut requested a review from a team as a code owner April 14, 2021 10:16
@pradn
Copy link
Contributor

pradn commented Apr 21, 2021

I took a first pass - looks reasonable. We need to check with Kamal if depending on the internals of a library class like this is fine. It seems to be helpful enough to the user to warrant this, but I wonder if so few are going to use this it might not be worth it.

@plamut
Copy link
Contributor Author

plamut commented Apr 22, 2021

Using as_completed() is pretty standard when dealing with multiple futures and waiting on their completion, it's not that niche a use case IMHO.

IIRC the feature request has been confirmed (and Node is adding something similar, too), but I agree we should discuss the approach in this PR - I'll add it to the agenda.

pradn
pradn previously approved these changes Apr 22, 2021
@pradn
Copy link
Contributor

pradn commented Apr 22, 2021

Looks good to me. It seems like we have consensus with the rest of the team, so perhaps simply ask for confirmation before merging?

@plamut
Copy link
Contributor Author

plamut commented Apr 22, 2021

Looks good to me. It seems like we have consensus with the rest of the team, so perhaps simply ask for confirmation before merging?

That's the plan.

And we might also borrow @jimfulton to have a say on using those implementation details, as he's been around Python ... probably since the fall of the Berlin Wall. 😆

(which might actually not even be far from the truth)

@plamut plamut dismissed pradn’s stale review April 22, 2021 17:55

Decided to also try swapping the futures with std lib's implementation first

@plamut
Copy link
Contributor Author

plamut commented Apr 22, 2021

After an offline discussion, we think relying on implementation details is not desirable, and the approach highest on the wish list is to swap the current implementation in the library with futures from concurrent.futures package.

The public interface is already compatible, but we need to see if that would break Pub/Sub Lite, and also determine the reason why the original authors decided to use their own implementation.

@dpcollins-google Does the Pub/Sub Lite client use any internals of the Futures, or just their public interface? Would replacing the futures with the implementation from concurrent.futures break any assumptions?

@plamut plamut added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Apr 22, 2021
@plamut
Copy link
Contributor Author

plamut commented May 15, 2021

Closing in favor of #397. We prefer less code and don't want to rely on the implementation details of concurrent.futures.Future.

@plamut plamut closed this May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the googleapis/python-pubsub API. cla: yes This human has signed the Contributor License Agreement. do not merge Indicates a pull request not ready for merge, due to either quality or timing.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make publish futures compatible with concurrent.futures.as_completed()
2 participants