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

Use MultiLoopChildWatcher in tests where available #5862

Merged
merged 2 commits into from
Jul 7, 2021

Conversation

sweatybridge
Copy link
Contributor

What do these changes do?

Python 3.8 adds a new child watcher class that doesn't require running loop in the main thread: https://docs.python.org/3/library/asyncio-policy.html#asyncio.MultiLoopChildWatcher. This PR uses the new watcher if it is available in asyncio module.

It should reduce test flakiness when parallelized with pytest-xdist.

Are there changes in behavior for the user?

No change for end users. Existing tests in test_loop.py covers this change given the current text matrix.

Related issue number

#3450
#2075

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jul 7, 2021
@webknjaz
Copy link
Member

webknjaz commented Jul 7, 2021

This is already being introduced in #5431 but with a threaded watcher. It's also being discussed in #5852. Why do you think that multiloop would be a better fit? Do you want to join those discussions instead?

@sweatybridge
Copy link
Contributor Author

ok, will join the other thread. Wasn't sure where to look since there are many related tickets.

@webknjaz
Copy link
Member

webknjaz commented Jul 7, 2021

Let's keep this open for now. I think we could accept it after some minor polishing. But I still need an answer to my question above, okay?

I encourage you to participate in other discussions, though. And it's indeed better to merge small PRs. I only mentioned other efforts so that people interested in achieving the common goal could collaborate more effectively and not repeat each other's work twice.

@webknjaz webknjaz reopened this Jul 7, 2021
Comment on lines 493 to 502
if hasattr(asyncio, "MultiLoopChildWatcher"):
watcher = asyncio.MultiLoopChildWatcher()
else:
watcher = asyncio.SafeChildWatcher()
Copy link
Member

Choose a reason for hiding this comment

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

I have a few comments on this block: it is preferable to use the EAFP style in Python and also we need to have comments about the motivation of this.

To solve this, I suggest you cherry-pick my commit bea959a (#5431) (same patch: https://github.com/aio-libs/aiohttp/commit/bea959a4ce195405939c40f184c5189206064447.patch) and then change ThreadedChildWatcher with MultiLoopChildWatcher in a follow-up commit. If you know how to use interactive rebase in Git, it should be easy for you :)

Then, we could merge this change and backport it.

After that, I think it would be useful to vendor a backported watcher right in this repo and make use of it under older supported Pythons too. But this is a separate effort that should go into a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review.

I'm reading this ticket https://bugs.python.org/issue38591 and there seems to be many instabilities with MultiLoopChildWatcher. I think ThreadedChildWatcher might actually be a better choice here...

Regarding the performance concerns of creating a new thread, how about additionally exposing skip_watcher: bool as a param? SafeChildWatcher was originally introduced to support asyncio.create_subprocess_*. In my use case, this support is not needed. If we expose the param, no one pays the cost unnecessarily.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, maybe that's why I chose ThreadedChildWatcher. I think I talked to Andrew and he said to use it. It was still problematic under the heavy load on my laptop, IIRC.

As for skip_watcher, please file a proposal issue or a draft PR to demonstrate how you see it being exposed, API-wise.

@webknjaz webknjaz enabled auto-merge (squash) July 7, 2021 12:35
@webknjaz webknjaz disabled auto-merge July 7, 2021 12:35
@codecov
Copy link

codecov bot commented Jul 7, 2021

Codecov Report

Merging #5862 (c6594ea) into master (5b23426) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #5862   +/-   ##
=======================================
  Coverage   96.75%   96.75%           
=======================================
  Files          44       44           
  Lines        9848     9851    +3     
  Branches     1591     1591           
=======================================
+ Hits         9528     9531    +3     
  Misses        182      182           
  Partials      138      138           
Flag Coverage Δ
unit 96.65% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiohttp/test_utils.py 99.68% <100.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b23426...c6594ea. Read the comment docs.

@webknjaz webknjaz changed the title Use MultiLoopChildWatcher when setting up test loop Use MultiLoopChildWatcher in tests where available Jul 7, 2021
@webknjaz webknjaz enabled auto-merge (squash) July 7, 2021 12:41
@webknjaz webknjaz merged commit 7080a8b into aio-libs:master Jul 7, 2021
@patchback
Copy link
Contributor

patchback bot commented Jul 7, 2021

Backport to 3.8: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.8/7080a8bba86db1ea38372183a90d82bfa7d6d8b1/pr-5862

Backported as #5863

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants