Error while using multiprocessing in Pygame
Question:
i’m making a text-based RPG and am trying to use multiproccessing to run both the pygame check function and the game function at the same time. This is my first time using multiprocessing so i’m not entirely sure what is going on.
Here is the (important) code:
from csv import reader
import pygame
from sys import exit
import multiprocessing
def check():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
def startgame():
play(input("Present Commandn"))
play_game_process = multiprocessing.Process(target=startgame)
play_game_process.start()
check_process = multiprocessing.Process(target=check)
check_process.start()
When i run the code, i am hit with a very large error message:
pygame 2.1.2 (SDL 2.0.18, Python 3.10.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.1.2 (SDL 2.0.18, Python 3.10.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.1.2 (SDL 2.0.18, Python 3.10.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 125, in _main
prepare(preparation_data)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:UsersDouglDocumentsCodingPythongamesCommandcommand.py", line 193, in <module>
play_game_process.start()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingprocess.py", line 121, in start
self._popen = self._Popen(self)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 336, in _Popen
return Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingpopen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.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.
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 125, in _main
prepare(preparation_data)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:UsersDouglDocumentsCodingPythongamesCommandcommand.py", line 193, in <module>
play_game_process.start()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingprocess.py", line 121, in start
self._popen = self._Popen(self)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 336, in _Popen
return Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingpopen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.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.
I am not sure what is going on here, please help if you can!
Answers:
Did you read the end of the error message, and the very clear instructions there? When using multiprocessing, your main code needs to be wrapped in the if __name__ == "__main__":
idiom (this is also documented in the multiprocessing
docs).
I.e. something like
import multiprocessing
from sys import exit
import pygame
def check():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
def startgame():
play(input("Present Commandn"))
def main():
play_game_process = multiprocessing.Process(target=startgame)
play_game_process.start()
check_process = multiprocessing.Process(target=check)
check_process.start()
if __name__ == '__main__':
main()
Note, however, that that probably won’t do what you want, since the check
and startgame
functions will be run in entirely different processes which don’t know about each other at all.
i’m making a text-based RPG and am trying to use multiproccessing to run both the pygame check function and the game function at the same time. This is my first time using multiprocessing so i’m not entirely sure what is going on.
Here is the (important) code:
from csv import reader
import pygame
from sys import exit
import multiprocessing
def check():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
def startgame():
play(input("Present Commandn"))
play_game_process = multiprocessing.Process(target=startgame)
play_game_process.start()
check_process = multiprocessing.Process(target=check)
check_process.start()
When i run the code, i am hit with a very large error message:
pygame 2.1.2 (SDL 2.0.18, Python 3.10.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.1.2 (SDL 2.0.18, Python 3.10.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
pygame 2.1.2 (SDL 2.0.18, Python 3.10.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 125, in _main
prepare(preparation_data)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:UsersDouglDocumentsCodingPythongamesCommandcommand.py", line 193, in <module>
play_game_process.start()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingprocess.py", line 121, in start
self._popen = self._Popen(self)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 336, in _Popen
return Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingpopen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.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.
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 125, in _main
prepare(preparation_data)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 289, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0librunpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:UsersDouglDocumentsCodingPythongamesCommandcommand.py", line 193, in <module>
play_game_process.start()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingprocess.py", line 121, in start
self._popen = self._Popen(self)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingcontext.py", line 336, in _Popen
return Popen(process_obj)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingpopen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0libmultiprocessingspawn.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.
I am not sure what is going on here, please help if you can!
Did you read the end of the error message, and the very clear instructions there? When using multiprocessing, your main code needs to be wrapped in the if __name__ == "__main__":
idiom (this is also documented in the multiprocessing
docs).
I.e. something like
import multiprocessing
from sys import exit
import pygame
def check():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
def startgame():
play(input("Present Commandn"))
def main():
play_game_process = multiprocessing.Process(target=startgame)
play_game_process.start()
check_process = multiprocessing.Process(target=check)
check_process.start()
if __name__ == '__main__':
main()
Note, however, that that probably won’t do what you want, since the check
and startgame
functions will be run in entirely different processes which don’t know about each other at all.