Search code examples
pythonpython-3.xmultiprocessingruntime-errorpython-multiprocessing

Multiprocessing throwing Runtime Error after executing p.start()


I am getting a Runtime Error after my code executes the p.start() method of Multiprocessing package in python.

Error which gets logged is as follows:

Traceback (most recent call last): File "/Users/anujpanchal/Documents/exportify-micro-booking/functionality/Notifications.py", line 396, in send_notification p.start() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 121, in start self._popen = self._Popen(self) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 224, in _Popen return _default_context.get_context().Process._Popen(process_obj) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 284, in _Popen return Popen(process_obj) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in init super().init(process_obj) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_fork.py", line 19, in init self._launch(process_obj) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 42, in _launch prep_data = spawn.get_preparation_data(process_obj._name) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 154, in get_preparation_data _check_not_importing_main() File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/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.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main exitcode = _main(fd, parent_sentinel) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 125, in _main prepare(preparation_data) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare _fixup_main_from_path(data['init_main_from_path']) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path main_content = runpy.run_path(main_path, File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 268, in run_path return _run_module_code(code, init_globals, run_name, File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code exec(code, run_globals) File "/Users/anujpanchal/Documents/exportify-micro-booking/functionality/Notifications.py", line 513, in Notifications(checkpoint="discharged_on_transshipment").send_notification(booking_id="XPF100283", tracking_obj=tracking_obj, scraper_update=False) File "/Users/anujpanchal/Documents/exportify-micro-booking/functionality/Notifications.py", line 470, in send_notification return response.errorResponse("Some error occurred please try again!") File "/Users/anujpanchal/Documents/exportify-micro-booking/functionality/response.py", line 50, in errorResponse return jsonify(response), status File "/Users/anujpanchal/Documents/exportify-micro-booking/venv/lib/python3.9/site-packages/flask/json/init.py", line 339, in jsonify if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] or current_app.debug: File "/Users/anujpanchal/Documents/exportify-micro-booking/venv/lib/python3.9/site-packages/werkzeug/local.py", line 348, in getattr return getattr(self._get_current_object(), name) File "/Users/anujpanchal/Documents/exportify-micro-booking/venv/lib/python3.9/site-packages/werkzeug/local.py", line 307, in _get_current_object return self.__local() File "/Users/anujpanchal/Documents/exportify-micro-booking/venv/lib/python3.9/site-packages/flask/globals.py", line 51, in _find_app raise RuntimeError(_app_ctx_err_msg) RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.app_context(). See the documentation for more information.

Why such a type of Runtime Error is being throwed, is it because I updated my MacBook to Big Sur 11.3 or is it because of something else?


Solution

  • I don't know too much about the Mac. In general, I believe most versions use (or used to use) OS call fork to create new processes. This meant that you did not have to put code that creates new processes within a block such as ...

    if __name__ == '__main__':
    

    ... which is required for platforms that use OS call spawn to create new processes, the reason being that when spawn is used the new process is created by initializing a new address space, launching a new Python interpreter and re-running the code from the very top of the program re-executing an code at global scope. This would result in re-executing in a recursive loop ad infinitum the very code that created the new process if that code were not conditionally executed by the above if statement.

    It would seem that in your update to Big Sur 11.3 the new default method of creating new processes has switched from fork to spawn -- and probably for a good reason. So I hesitate to suggest that you first try calling ...

    multiprocessig.set_start_method('fork')
    

    ... to restore fork as the default method to use for creating new processes.

    Instead, I would suggest that you enclose your process-creation code in the aforementioned if block. It's really unfortunate that you decided not to post any of your code or else I could have showed you exactly how to do that.