Repository navigation
Switch default multiprocessing context to "spawn" - #4003
Conversation
|
I'm curious, how does this affect performance? My guess is that it would pretty negatively affect things for modestly sized tasks. |
|
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. |
|
I wrote a little test program to check the above.
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 |
|
Oh, and I should note that |
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? |
|
I'm not very well informed on this topic though. It would be great if some other maintainers chimed in here. |
|
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/ |
|
Dask.distributed workers have an intermittent failure like that as well in
Python 2.
However I've never run into anything in practice when using the
multiprocessing scheduler. I wonder if that doesn't run into the same
issues somehow? I would have expected it to have arisen in a bug report by
now.
…On Tue, Sep 25, 2018 at 10:54 AM Itamar Turner-Trauring < ***@***.***> wrote:
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/
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#4003 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AASszCg4VFrT4E_PubrAbFNpDEF9Y-LGks5uekOOgaJpZM4W0taO>
.
|
|
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 As a user I would either expect the defaults to be the same across platforms ( |
|
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 |
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. |
|
Another alternative: warn in visible way if users are using current default (permanently, or as prelude to eventual merge of this PR). |
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. |
|
OK. |
|
@jakirkham said he is in favor of making this change, this time around (#3759 (comment)). |
|
To reiterate what I said there, we are already doing this in Distributed ( dask/distributed#3461 ). This just does the same thing in Dask. |
|
Thanks for reopening. IMO distributed using spawn by default tips the scales to also using |
|
Seems to be some related test failures https://github.com/dask/dask/pull/4003/checks?check_run_id=616399950#step:6:197 |
| 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") |
There was a problem hiding this comment.
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).
|
Appears there is a linter failure. Could you please run |
|
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. |
|
I wouldn't wait for me on a decision here
…On Mon, Apr 27, 2020 at 8:42 AM Jim Crist-Harif ***@***.***> wrote:
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.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4003 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACKZTFFISDEZWGUYHKKCWTROWRXHANCNFSM4FWS22HA>
.
|
|
Thanks! |
|
Thanks @itamarst for the PR and everyone for the reviews! 😄 |
flake8 daskFixes #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.cloudpickleprobably makes this unnecessary, though?