Improve error when if __name__ == "__main__" is omitted
See original GitHub issueWhat happened:
When running a small future submitting example the cluster fails to start and dumps reams of tracebacks.
What you expected to happen:
Prints the result.
Minimal Complete Verifiable Example:
$ conda create -n dask-what python dask distributed -y
$ conda activate dask-what
# what.py
from dask.distributed import Client
f = lambda: 10 + 1
with Client() as client:
print(client.submit(f).result())
$ python what.py
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
2022-08-01 10:44:48,185 - distributed.nanny - ERROR - Failed to start process
Full traceback
/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/node.py:179: UserWarning: Port 8787 is already in use.
Perhaps you already have a cluster running?
Hosting the HTTP server on port 41555 instead
warnings.warn(
/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/node.py:179: UserWarning: Port 8787 is already in use.
Perhaps you already have a cluster running?
Hosting the HTTP server on port 40205 instead
warnings.warn(
/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/node.py:179: UserWarning: Port 8787 is already in use.
Perhaps you already have a cluster running?
Hosting the HTTP server on port 37055 instead
warnings.warn(
/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/node.py:179: UserWarning: Port 8787 is already in use.
Perhaps you already have a cluster running?
Hosting the HTTP server on port 40399 instead
warnings.warn(
2022-08-01 10:44:48,184 - distributed.nanny - ERROR - Failed to start process
Traceback (most recent call last):
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/nanny.py", line 436, in instantiate
result = await self.process.start()
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/nanny.py", line 679, in start
await self.process.start()
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/process.py", line 38, in _call_and_set_future
res = func(*args, **kwargs)
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/process.py", line 199, in _start
process.start()
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/context.py", line 284, in _Popen
return Popen(process_obj)
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 32, in __init__
super().__init__(process_obj)
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/popen_fork.py", line 19, in __init__
self._launch(process_obj)
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 42, in _launch
prep_data = spawn.get_preparation_data(process_obj._name)
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/spawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/multiprocessing/spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
2022-08-01 10:44:48,185 - distributed.nanny - ERROR - Failed to start process
... repeated many times until keyboard interrupt
task: <Task pending name='Task-77' coro=<Nanny._on_exit() running at /home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/utils.py:778> wait_for=<Future pending cb=[Task.task_wakeup()]> cb=[set.remove()]>
Exception ignored in: <coroutine object Nanny._on_exit at 0x7f5117e3c120>
Traceback (most recent call last):
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/utils.py", line 777, in wrapper
File "/home/jtomlinson/miniconda3/envs/dask-what/lib/python3.10/site-packages/distributed/utils.py", line 792, in __exit__
ImportError: sys.meta_path is None, Python is likely shutting down
... repeated many times
Issue Analytics
- State:
- Created a year ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
code doesnt run when using if __name__ == '__main__'
The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.. When adding if name...
Read more >Add if __name__ == '__main__' When Spawning Processes
You can fix a RuntimeError when starting child processes by adding if name == 'main' to checking for the top-level environment in your...
Read more >How can I use "if __name__ == '__main__':" in my code below?
I wanted to know how and where I should add “if name == 'main':” in my code below: from scipy import stats, optimize...
Read more >How to correct a #NAME? error - Microsoft Support
The top reason why the #NAME? error appears in the formula is because there is a typo in the formula name. Look at...
Read more >Python Tutorial - if __name__ == '__main__' - BogoToBogo
We see if __name__ == '__main__': quite often. It checks if a module is being imported or not. In other words, the code...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed it! I needed
if __name__ == "__main__":
.Thanks @jrbourbeau I hadn’t managed to discover #2708 despite searching for duplicates. That’s definitely the right place to track this.