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

Add parallel building support #29

Closed
wants to merge 3 commits into from
Closed

Add parallel building support #29

wants to merge 3 commits into from

Conversation

xmo-odoo
Copy link

And also add testing support and a few tests and some updates to e.g. logging / printing. One thing the tests don't cover is whether updates are handled properly (aka the sitemap is complete) but I'm not too sure how to test this, the test for emptyness is also skipped (for now).

Anyway the meat is the opting into parallel mode, because on a relatively large project using a parallel-safe builder adding sphinx-sitemap to the mix basically multiplies the (wallclock) build time by 5, most of which is the loss of parallel reading and writing.

Turns out sitemap was always parallel-read-safe because it really doesn't do anything during read so I could have flipped that flag and called it a day, but by then I was in a bit too deep and I added parallel writing support. This is achieved by storing a queue in the env so the "write" workers can shove page names in there (sadly multiprocessing.Queue did not work as it apparently doesn't like the way the workers are created), and create_sitemap can then pop all that from the queue.

Since it goes through a manager, I could probably have gone with a list and not changed the interface, now that I think about it. Oh well.

Anyway there's a bit of an issue here, probably not a huge one because sitemap is only invoked once per document (not hundreds of times per), but the multiprocessing queue is always created and possibly somewhat heavy (it's at least a separate process, plus whatever the proxies use to communicate with the subprocess), but while Builder has a parallel_ok flag that flag is only set during build(), which comes later than record_builder_type.

The only option I can see (aside from Sphinx being updated to resolve this earlier) is to copy the implementation details of parallel_ok in the extension and hope they don't change too much in the future, using that to decide between the seq and the parallel sitemap_links implementations, and that's gross.

Even hooking into one of the events between the reading and writing phase would not work: parallel_ok is actually set after env-check-consistency, and there doesn't seem to be any sequential event afterwards.

xmo-odoo added 3 commits July 15, 2021 14:09
Leverage Tox to "test" on all Python versions 3.6 and up, as well as
all major Sphinx versions above 1.2 (include running tests against
Sphinx's latest version if above 4 just in case).

Add basic testing structure patterned after Sphinx's own
`test_ext_todo`. The `test-root` pseudo-doc has 7 documents in order
to ensure parallel reading will trigger (requires at least 5
documents) without needing to update the pseudo-doc or create a
separate one.
Add support for Application-bound methods (`Sphinx.info`,
Sphinx.warn`) for 1.2 compatibility as `sphinx.util.logging` was
really only added in 1.6. Alternatively maybe the error should plain
raise an error?

Also allows adding some testing around those error and warning
conditions.
Technically the extension is *already* parallel-read-safe because it's
not implicated in reading at all, its "reading" work occurs during
writing.

As preparatory code clean up the code a bit:

* remove `app.locales` entirely, the locales fetching only occurs
  during the final writing so `get_locales` can return the information
* update some etree calls to use kwargs

For the multiprocessing, `add_html_link` can be called from different
worker processes while `create_sitemap` is always called from a single
one. While the env is fanned out when creating workers (so they can
all see the env) unlike reading there is no merge step for
writing. Therefore an other synchronisation mechanism is needed to
feed worker information back to the parent.

`multiprocessing.Queue` didn't work (it complains about inheritance
between processes), while likely quite a bit heavier
`multiprocessing.Manager.Queue` does, so use that: set the queue on
the `env`, fill the queue on `add_html_link`, and pull from the queue
until it's empty when generating the sitemap.

One unfortunate issue is the deoptimisation of serial writing: a
managed queue is always created which is much heavier than a list (in
my understanding the manager is essentially a separate process which
manages synchronisation between the various users of the
proxy). Conditionally using a managed queue seems difficult: there is
a `Builder.parallel_ok` flag but it's only set during `build()`, so
not set at creation. We could hand-roll the corresponding checks, but
they're mostly implementation details which is not convenient.
jdillard added a commit that referenced this pull request Dec 25, 2022
Huge shout-out to @xmo-odoo for #29 (closed so couldn't merge).

Future improvements to detecting when parallel mode is turned on could be made, as mentioned in the PR:

> This is achieved by storing a queue in the env so the "write" workers can shove page names in there (sadly multiprocessing.Queue did not work as it apparently doesn't like the way the workers are created), and create_sitemap can then pop all that from the queue.
>
> Anyway there's a bit of an issue here, probably not a huge one because sitemap is only invoked once per document (not hundreds of times per), but the multiprocessing queue is always created and possibly somewhat heavy (it's at least a separate process, plus whatever the proxies use to communicate with the subprocess), but while Builder has a parallel_ok flag that flag is only set during build(), which comes later than record_builder_type.
>
> The only option I can see (aside from Sphinx being updated to resolve this earlier) is to copy the implementation details of parallel_ok in the extension and hope they don't change too much in the future, using that to decide between the seq and the parallel sitemap_links implementations, and that's gross.
>
> Even hooking into one of the events between the reading and writing phase would not work: parallel_ok is actually set after env-check-consistency, and there doesn't seem to be any sequential event afterwards.

Potentially better support from Sphinx outlined here: sphinx-doc/sphinx#9480
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant