Skip to content

Improve asyncio-dev 'Concurrency and Multithreading' docs #20882

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

Merged
merged 3 commits into from
Aug 29, 2020
Merged

Improve asyncio-dev 'Concurrency and Multithreading' docs #20882

merged 3 commits into from
Aug 29, 2020

Conversation

akindofyoga
Copy link
Contributor

@akindofyoga akindofyoga commented Jun 15, 2020

I added some information to the Concurrency and Multithreading section of the Developing with asyncio guide.

This is all information that would have helped me when I started using asyncio. I incorrectly assumed that loop.call_soon_threadsafe() and run_coroutine_threadsafe() could be called from a thread in a process separate from the one that the event loop is running in. Explicitly stating that this will not work will probably help some people starting out with asyncio in the future.

I also added references to some other functions that can be used for inter-process communication without blocking the event loop. The section already mentions running blocking code in a ThreadPoolExecutor, but I think listing these other options in this section will also be helpful.

Automerge-Triggered-By: @aeros

@akindofyoga
Copy link
Contributor Author

@aeros Do you have any idea who might be willing to review this change?

@aeros aeros changed the title Improve asyncio documentation Improve asyncio-dev 'Concurrency and Multithreading' docs Jun 24, 2020
@aeros aeros self-requested a review June 24, 2020 08:12
@aeros
Copy link
Contributor

aeros commented Jun 24, 2020

I should have time to consider and review these potential changes in the relatively near future. Thanks for the PR.

@akindofyoga
Copy link
Contributor Author

@aeros Just sending you a friendly ping about this.

Copy link
Contributor

@aeros aeros left a comment

Choose a reason for hiding this comment

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

+1. I'm considering whether or not we want to say "There is no way" or "There is currently no way", because it is very possible that high-level abstraction could be provided over the OS pipes in order to facilitate scheduling callbacks and running coroutines from within a different process, so I'm leaning a bit towards currently. There are not immediate plans to implement something like this, but it's not entirely unfeasible if we can establish clear real-world use cases for something like that (separate from what can already be accomplished with loop.run_in_executor(<PPE_INSTANCE>, func, ...) or the subprocesses API) and if people are interested in working on it.

However, I think this section provides a needed explanation about this topic that doesn't currently exist, so I would be fine with adding it as is. I'll give @1st1 a couple of weeks (from now) to look over it prior to merging though. Feel free to send me another reminder ping if the PR is still open after that without any updates or requested changes. :-)

@akindofyoga
Copy link
Contributor Author

Thanks for the prompt response about this! I changed "There is no way" to "There is currently no way."

As I mentioned earlier, this information would have helped me out a lot when I first got started with asyncio.

@akindofyoga
Copy link
Contributor Author

@aeros Sending you a reminder ping.

@aeros
Copy link
Contributor

aeros commented Aug 29, 2020

Sorry for the delay, I'll proceed with merging this. Just need to close and re-open to restart the CI (it looks like Travis was stalled last time it ran). Thanks again for the PR!

@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: Required status check "Travis CI - Pull Request" is in progress..

@miss-islington miss-islington merged commit c68c5af into python:master Aug 29, 2020
@miss-islington
Copy link
Contributor

Thanks @rogerthat94 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 29, 2020
…0882)

I added some information to the `Concurrency and Multithreading` section of the `Developing with asyncio` guide.

This is all information that would have helped me when I started using asyncio. I incorrectly assumed that `loop.call_soon_threadsafe()` and `run_coroutine_threadsafe()` could be called from a thread in a process separate from the one that the event loop is running in. Explicitly stating that this will not work will probably help some people starting out with asyncio in the future.

I also added references to some other functions that can be used for inter-process communication without blocking the event loop. The section already mentions running blocking code in a ThreadPoolExecutor, but I think listing these other options in this section will also be helpful.
(cherry picked from commit c68c5af)

Co-authored-by: Roger Iyengar <ri@rogeriyengar.com>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Aug 29, 2020
@bedevere-bot
Copy link

GH-22009 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 29, 2020
…0882)

I added some information to the `Concurrency and Multithreading` section of the `Developing with asyncio` guide.

This is all information that would have helped me when I started using asyncio. I incorrectly assumed that `loop.call_soon_threadsafe()` and `run_coroutine_threadsafe()` could be called from a thread in a process separate from the one that the event loop is running in. Explicitly stating that this will not work will probably help some people starting out with asyncio in the future.

I also added references to some other functions that can be used for inter-process communication without blocking the event loop. The section already mentions running blocking code in a ThreadPoolExecutor, but I think listing these other options in this section will also be helpful.
(cherry picked from commit c68c5af)

Co-authored-by: Roger Iyengar <ri@rogeriyengar.com>
@bedevere-bot
Copy link

GH-22010 is a backport of this pull request to the 3.8 branch.

@graingert
Copy link
Contributor

graingert commented Aug 30, 2020

@rogerthat94 interesting, this should probably kick off a ticket to make the loop pickle-able, eg to define __setstate__ and __getstate__ so that call_soon_threadsafe can be run from a multiprocessing subprocess

@akindofyoga
Copy link
Contributor Author

@aeros Thank you for all of your help!

@graingert Do you want me to create that ticket? Another class that might be worth adding is a version of multiprocessing.Pipe that won't block the event loop when you read from it. I know how to make this, and would be interested in doing so. Do you think there would be interest in this?

@akindofyoga akindofyoga deleted the patch-1 branch August 30, 2020 03:04
@aeros
Copy link
Contributor

aeros commented Aug 30, 2020

@graingert

interesting, this should probably kick off a ticket to make the loop pickle-able, eg to define setstate and getstate so that call_soon_threadsafe can be run from a multiprocessing subprocess

I'd suggest to also start a thread on python-ideas with the proposal of allowing loop.call_soon_threadsafe() (or potentially a new member) support callbacks from other processes if it's something you'd like to see; it's important to detail some concrete real-world use cases that you might apply it to. However, I suspect it would involve a bit more work than simply defining __setstate__ and __getstate__ for the default asyncio event loops (Selector and Proactor), as they are mainly intended to work within a single thread (specifically, one event loop per thread). That would only satisfy the ability to pass it to another process, but not necessarily allow it to work properly within a different process from where it originated.

That's not always strictly required for enhancements/new features, but it's a good way to attract attention to it and make a strong argument for why it should be added (and maintained potentially indefinitely in the stdlib). The goal should mainly be to convince @1st1 and/or @asvetlov, since they're the primary experts/maintainers for asyncio.

Not that I don't think it would be useful to have, it's more of a question of whether it would be widely used enough to justify supporting it within asyncio (which is primarily targeted at an event loop operating within a single thread, with some interoperability measures to support working with multiple threads). At the moment, the main bridge between multiprocessing and asyncio is really just loop.run_in_executor() with passing an instance of ProcessPoolExecutor().

@rogerthat94

Thank you for all of your help!

No problem. :-)

Do you want me to create that ticket? Another class that might be worth adding is a version of multiprocessing.Pipe that won't block the event loop when you read from it. I know how to make this, and would be interested in doing so. Do you think there would be interest in this?

The above part about python-ideas also applies to the idea of adding a non-blocking version of multiprocessing.Pipe, perhaps more so since it would proposing to add a new class.

miss-islington added a commit that referenced this pull request Sep 4, 2020
…0882) (GH-22010)

I added some information to the `Concurrency and Multithreading` section of the `Developing with asyncio` guide. 

This is all information that would have helped me when I started using asyncio. I incorrectly assumed that `loop.call_soon_threadsafe()` and `run_coroutine_threadsafe()` could be called from a thread in a process separate from the one that the event loop is running in. Explicitly stating that this will not work will probably help some people starting out with asyncio in the future.

I also added references to some other functions that can be used for inter-process communication without blocking the event loop. The section already mentions running blocking code in a ThreadPoolExecutor, but I think listing these other options in this section will also be helpful.
(cherry picked from commit c68c5af)


Co-authored-by: Roger Iyengar <ri@rogeriyengar.com>
miss-islington added a commit that referenced this pull request Sep 4, 2020
…0882) (GH-22009)

I added some information to the `Concurrency and Multithreading` section of the `Developing with asyncio` guide. 

This is all information that would have helped me when I started using asyncio. I incorrectly assumed that `loop.call_soon_threadsafe()` and `run_coroutine_threadsafe()` could be called from a thread in a process separate from the one that the event loop is running in. Explicitly stating that this will not work will probably help some people starting out with asyncio in the future.

I also added references to some other functions that can be used for inter-process communication without blocking the event loop. The section already mentions running blocking code in a ThreadPoolExecutor, but I think listing these other options in this section will also be helpful.
(cherry picked from commit c68c5af)


Co-authored-by: Roger Iyengar <ri@rogeriyengar.com>
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
…0882)

I added some information to the `Concurrency and Multithreading` section of the `Developing with asyncio` guide. 

This is all information that would have helped me when I started using asyncio. I incorrectly assumed that `loop.call_soon_threadsafe()` and `run_coroutine_threadsafe()` could be called from a thread in a process separate from the one that the event loop is running in. Explicitly stating that this will not work will probably help some people starting out with asyncio in the future.

I also added references to some other functions that can be used for inter-process communication without blocking the event loop. The section already mentions running blocking code in a ThreadPoolExecutor, but I think listing these other options in this section will also be helpful.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants