Skip to content

Switch default multiprocessing context to "spawn" - #4003

Merged
TomAugspurger merged 7 commits into
dask:masterfrom
itamarst:better-multiprocessing-context-3759
Apr 27, 2020
Merged

TomAugspurger merged 7 commits into
dask:masterfrom
itamarst:better-multiprocessing-context-3759

Conversation

@itamarst

@itamarst itamarst commented Sep 21, 2018 •

Copy link
Copy Markdown
Contributor
  • Tests added / passed
  • Passes flake8 dask

Fixes #3759.

Note that this may require some code to use if __name__ == '__main__': to start dask (https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods) in single module programs. cloudpickle probably makes this unnecessary, though?

@mrocklin

Copy link
Copy Markdown
Member

I'm curious, how does this affect performance? My guess is that it would pretty negatively affect things for modestly sized tasks.

@itamarst

Copy link
Copy Markdown
Contributor Author

It only affects startup. Once the pool is started it doesn't make a difference. If pool startup is a bottleneck you probably shouldn't be using Dask, so that doesn't seem like a big deal to me.

@itamarst

itamarst commented Sep 24, 2018 •

Copy link
Copy Markdown
Contributor Author

I wrote a little test program to check the above.

  1. This PR does seem to make if __name__ == __main__: necessary for single module example to work, it seems.
  2. The performance overhead is just small additional fixed cost at startup.
import time
import sys
from dask.bag import from_sequence
from dask import compute, config

def expensive(x):
    time.sleep(1)
    return x * 2

if __name__ == '__main__':
    with config.set({"multiprocessing.context": sys.argv[1]}):
        start = time.time()
        bag = from_sequence(range(90)).map(expensive).sum()
        print(compute(bag, npartitions=8))
        print(f"Elapsed with method {sys.argv[1]}", time.time() - start)
$ python3 benchmark.py fork
(8010,)
Elapsed with method fork 12.09099531173706
$ python3 benchmark.py spawn
(8010,)
Elapsed with method spawn 12.255864143371582

@itamarst

Copy link
Copy Markdown
Contributor Author

Oh, and I should note that if __name__ == '__main__': is almost certainly already required if you want examples to work on Windows, since Windows uses spawn method by default. So I don't think this is an additional burden.

@mrocklin

Copy link
Copy Markdown
Member

The performance overhead is just small additional fixed cost at startup.

The tasks in your benchmark take 1s. What if instead they take 10ms? Is the cost still negligible?

I find myself not particularly excited about this change because I don't think that I've ever heard of a user running into a problem with the fork solution. I'm curious @itamarst have you run into issues with Dask's use of fork in practice, or are you promoting the use of good technology? Are there common situations that you suspect occur frequently among dask users where this is an issue?

@mrocklin

Copy link
Copy Markdown
Member

I'm not very well informed on this topic though. It would be great if some other maintainers chimed in here.

@itamarst

Copy link
Copy Markdown
Contributor Author

I have encountered—not in Dask, but in other uses of multiprocessing—random freezes that were caused by the default method, as well as messed up logging behavior. You won't always see them yourself because they're race conditions, but basically the experience is "my program is acting wierd and I have no idea why." Most users will just give up, or just live with things freezing 1% of the time.

Basically, the default method is fundamentally broken. See the original issue, and a much longer writeup I did: https://codewithoutrules.com/2018/09/04/python-multiprocessing/

@mrocklin

mrocklin commented Sep 25, 2018 via email

Copy link
Copy Markdown
Member

@jcrist

jcrist commented Sep 26, 2018

Copy link
Copy Markdown
Member

I don't expect performance problems with this change (creating the pool should never be the bottleneck), but if some users are currently relying on fork behavior then their code may error with this change. For example, with the fork context I don't have to surround my code with if __name__ == '__main__':, but I do with the spawn context. While relying on fork isn't cross platform, if I'm just trying to do some computations locally I likely haven't worried about that previously - this change would break my code.

As a user I would either expect the defaults to be the same across platforms (spawn), or match python's defaults (fork on *nix, spawn on windows). Since switching to spawn may break existing user's code, I'm not sure if we should make this change.

@itamarst

Copy link
Copy Markdown
Contributor Author

This change might break your code... but your code was previously broken some percentage of the time already...

Would it be acceptable if lack of if __name__ == '__main__' resulted in a clear error indicating the fix? Might be possible to detect that (maybe).

@jcrist

jcrist commented Sep 26, 2018

Copy link
Copy Markdown
Member

but your code was previously broken some percentage of the time already

This isn't necessarily true. Perhaps I only care about posix, and only use modules with fork-safe behavior. I suspect a non-negligible portion of dask users don't care about writing code that deploys on every OS, only that their code works on their machine.

I'm not 100% against this, but in 3.5 years we've never had a user run into this issue when using dask. I'm hesitant to make changes that will break user's code without clear evidence that the existing behavior is bad (and I'm not currently convinced that it is).

That said, I suspect at this point that most users use either the threaded scheduler or the distributed scheduler - the distributed scheduler runs fine locally, and is more efficient in many cases than the multiprocessing scheduler. Breaking code for a few to save many from having future issues may be worth it still.

@itamarst

Copy link
Copy Markdown
Contributor Author

Another alternative: warn in visible way if users are using current default (permanently, or as prelude to eventual merge of this PR).

@mrocklin

Copy link
Copy Markdown
Member

warn in visible way if users are using current default

Building on what @jcrist has said above, the use of fork doesn't really seem to be bothering users today or stopping them from being productive. We get bug reports about lots of parts of the system, but this topic has never come up. I hesitate to teach people and push them away from a system that, pragmatically at least, seems to work ok today. There is a very real cost to raising a warning to every user that I don't think is matched by any perceived benefits here. I would want to revisit this if we found that many people were having difficulty due to using the defaults.

@itamarst

itamarst commented Oct 1, 2018

Copy link
Copy Markdown
Contributor Author

OK.

@itamarst

Copy link
Copy Markdown
Contributor Author

@jakirkham said he is in favor of making this change, this time around (#3759 (comment)).

Comment thread dask/multiprocessing.py Resolved
@jakirkham

Copy link
Copy Markdown
Member

To reiterate what I said there, we are already doing this in Distributed ( dask/distributed#3461 ). This just does the same thing in Dask.

@TomAugspurger

Copy link
Copy Markdown
Member

Thanks for reopening. IMO distributed using spawn by default tips the scales to also using spawn here, but we'll wait for feedback from others before merging..

Comment thread docs/source/scheduler-overview.rst Resolved
@TomAugspurger

Copy link
Copy Markdown
Member

Seems to be some related test failures https://github.com/dask/dask/pull/4003/checks?check_run_id=616399950#step:6:197

____________________ test_custom_context_ignored_elsewhere ____________________

self = <multiprocessing.context.DefaultContext object at 0x000001BE1AF94518>
method = 'forkserver'

    def get_context(self, method=None):
        if method is None:
            return self
        try:
>           ctx = _concrete_contexts[method]
E           KeyError: 'forkserver'

and https://travis-ci.org/github/dask/dask/jobs/679167905#L1057

Comment thread dask/multiprocessing.py
context_name = "fork"
# fork context does fork()-without-exec(), which can lead to deadlocks,
# so default to "spawn".
context_name = config.get("multiprocessing.context", "spawn")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah ok. Seeing the test failure maybe we should just readd the previous Windows check after this line (tweaking it to check if it is not "spawn" instead).

@jakirkham

Copy link
Copy Markdown
Member

Appears there is a linter failure. Could you please run black locally and push the changes?

@TomAugspurger TomAugspurger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cc @mrocklin and @jcrist. I'm guess your opinions have changed now that distributed is using it, but want to confirm.

@jcrist

jcrist commented Apr 27, 2020

Copy link
Copy Markdown
Member

Yeah, I'm fine with this now. I think at this point we've successfully dissuaded most use of the multiprocessing scheduler in favor of distributed (which is significantly more efficient), so this change probably affects only a few users anyway.

@mrocklin

mrocklin commented Apr 27, 2020 via email

Copy link
Copy Markdown
Member

@TomAugspurger
TomAugspurger merged commit ae42c72 into dask:master Apr 27, 2020
@TomAugspurger

Copy link
Copy Markdown
Member

Thanks!

@jakirkham

Copy link
Copy Markdown
Member

Thanks @itamarst for the PR and everyone for the reviews! 😄

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.

Default multiprocessing context is broken and should never be used

5 participants