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

gh-85299: Add note warning about entry point guard for asyncio example #93457

Merged
merged 5 commits into from
Oct 16, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,13 @@ Executing code in thread or process pools
pool, cpu_bound)
print('custom process pool', result)

asyncio.run(main())
if __name__ == '__main__':
asyncio.run(main())

Note that on Windows and macOS, the entry point guard may be necessary as the
example would otherwise cause a :exc:`RuntimeError`. For more details, see the
`Safe importing of main module
<https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods>`_.
Copy link
Member

@gvanrossum gvanrossum Oct 15, 2022

Choose a reason for hiding this comment

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

I propose to simplify the note to the following:

Note that the entry point guard (if __name__ == '__main__') is required for option 3, due to the peculiarities of multiprocessing, which is used by ProcessPoolExecutor. See (link).

There should be three cross-references:

  • multiprocessing (the module)
  • ProcessPoolExecutor (the class)
  • Safe importing of main module

For the latter I recommend to add a label to the target section in multiprocessing.rst rather than linking to the website. (@CAM-Gerlach can provide details on how to do this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding though is that option 3 fails when spawn/forkserver is the process start method, and only Windows + mac default to spawn, while unix defaults to fork, so saying 'is required' may be a bit too strong. If 'may be required' instead, then the reader might wonder why 'may', in which case I think a link to https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods somewhere would help explain things.

Copy link
Member

Choose a reason for hiding this comment

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

That's too subtle for the readers. We're not stating the law, we're just explaining the example.

Copy link
Member

Choose a reason for hiding this comment

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

See my comment for a suggestion implementing the above and the details Guido mentioned.

slateny marked this conversation as resolved.
Show resolved Hide resolved

This method returns a :class:`asyncio.Future` object.

Expand Down