bpo-32309: Implement asyncio.to_thread() by aeros · Pull Request #20143 · python/cpython (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation33 Commits9 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

aeros

Implements asyncio.to_thread, a coroutine for asynchronously running IO-bound functions in a separate thread without blocking the event loop. See the discussion starting from here in GH-18410 for context.

https://bugs.python.org/issue32309

Automerge-Triggered-By: @aeros

@aeros @1st1

Co-authored-by: Yury Selivanov yury@edgedb.com

aeros

@blurb-it

cjerdonek

@aeros

@aeros @cjerdonek

Co-authored-by: Chris Jerdonek chris.jerdonek@gmail.com

@aeros

@aeros

@aeros

1st1

1st1 approved these changes May 18, 2020

@1st1

1st1

@tmewett

Is the name finalised? I saw the discussion about run_in_thread being a bit ambiguous but personally I don't think to_thread is very clear either.

I would suggest create_thread. Then, this would match up almost exactly with the semantics of asyncio.create_task

create_task: Wrap the coroutine into a Task and schedule its execution. Return the Task object.

create_thread: Wrap the function in a thread and start its execution. Return a future object.

If you see what I mean? The functions are very similar but one is for coros concurrently in the event loop and the other is for functions concurrently in threads.

cjerdonek

@cjerdonek

Thanks. @tmewett I think the proposed name is good enough. Re: create_thread(), I think it has the issue that when backed by a pool, it's not necessarily creating a new thread. With to_thread(), you can think of it as "going to" a thread in the pool.

asvetlov

Choose a reason for hiding this comment

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

I have a little doubt for the documentation text; the code itself and test are fine.

@1st1

@aeros Looks like everybody approves this so you can go ahead and merge!

Next todo:

@aeros

@aeros Looks like everybody approves this so you can go ahead and merge!

Awesome, thanks for the reviews @1st1, @cjerdonek, and @asvetlov!

I'll discuss the documentation a bit more w/ Andrew regarding the examples, and then proceed w/ merging it.

@aeros

methane

@aeros @asvetlov

Co-authored-by: Andrew Svetlov andrew.svetlov@gmail.com

@aeros

Thanks for the review as well @methane. :-)

@aeros

Status check is done, and it's a failure x .

This occurred from a minor doctest warning, apparently having tildes in the code comments of example code is flagged as "suspicious". The latest commit should address it.

@miss-islington

@aeros: Status check is done, and it's a success ✅ .

@zware

Note that this missed the 3.9 boat; it's now merged into 3.10. The whatsnew entry should be moved accordingly :)

(The documentation needs the appropriate .. versionadded: 3.10 marker, as well.)

@aeros

Note that this missed the 3.9 boat; it's now merged into 3.10. The whatsnew entry should be moved accordingly :)

Currently, I'm reaching out to Łukasz to see if we could consider including this in 3.9b2 since it was so close to the deadline (it's been a WIP since last year and would have otherwise made it on-time, if not for some last minute changes). If he decides that it's too late, I'll update the whatsnew entry.

(I'll also add the versionadded marker as well, good catch.)

@miss-islington

Thanks @aeros for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@bedevere-bot

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request

May 19, 2020

@aeros @miss-islington

@aeros

Thanks, @ambv!

I'll try to open a PR to add the versionadded to the docs in the next couple of days, as @zware mentioned earlier.

JrooTJunior

@aeros aeros mentioned this pull request

May 21, 2020

miss-islington pushed a commit that referenced this pull request

May 21, 2020

@aeros

Allows contextvars from the main thread to be accessed in the separate thread used in asyncio.to_thread(). See the [discussion](#20143 (comment)) in GH-20143 for context.

Automerge-Triggered-By: @aeros

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request

May 21, 2020

@aeros @miss-islington

arturoescaip pushed a commit to arturoescaip/cpython that referenced this pull request

May 24, 2020

@aeros @arturoescaip

Implements asyncio.to_thread, a coroutine for asynchronously running IO-bound functions in a separate thread without blocking the event loop. See the discussion starting from [here](python#18410 (comment)) in pythonGH-18410 for context.

Automerge-Triggered-By: @aeros

arturoescaip pushed a commit to arturoescaip/cpython that referenced this pull request

May 24, 2020

@aeros @arturoescaip

Reviewers

@1st1 1st1 1st1 approved these changes

@cjerdonek cjerdonek cjerdonek approved these changes

@asvetlov asvetlov asvetlov approved these changes

@JrooTJunior JrooTJunior JrooTJunior left review comments

@methane methane methane approved these changes