AminHP / gym-mtsim

A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)
MIT License
412 stars 101 forks source link

issue with multi processing #12

Closed sadimoodi closed 2 years ago

sadimoodi commented 2 years ago

Hello @AminHP , when running one of your examples (Create an environment with custom parameters), i get the follwoing error: File "", line 1, in File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\spawn.py", line 116, in spawn_main exitcode = _main(fd, parent_sentinel) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\spawn.py", line 125, in _main prepare(preparation_data) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\spawn.py", line 236, in prepare _fixup_main_from_path(data['init_main_from_path']) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\spawn.py", line 287, in _fixup_main_from_path main_content = runpy.run_path(main_path, File "C:\Program Files\Python39\lib\runpy.py", line 268, in run_path return _run_module_code(code, init_globals, run_name, File "C:\Program Files\Python39\lib\runpy.py", line 97, in _run_module_code _run_code(code, mod_globals, init_globals, File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "c:\Users\Ali.Khankan\Desktop\gym-mtsim\ali_test.py", line 16, in env = MtEnv( File "c:\Users\Ali.Khankan\Desktop\gym-mtsim\gym_mtsim\envs\mt_env.py", line 63, in init self.multiprocessing_pool = Pool(multiprocessing_processes) if multiprocessing_processes else None File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\pathos\multiprocessing.py", line 111, in init self._serve() File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\pathos\multiprocessing.py", line 123, in _serve _pool = Pool(nodes) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\pool.py", line 212, in init self._repopulate_pool() File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\pool.py", line 303, in _repopulate_pool return self._repopulate_pool_static(self._ctx, self.Process, File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\pool.py", line 326, in _repopulate_pool_static w.start() File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\process.py", line 121, in start self._popen = self._Popen(self) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\context.py", line 327, in _Popen return Popen(process_obj) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\popen_spawn_win32.py", line 45, in init prep_data = spawn.get_preparation_data(process_obj._name) File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\spawn.py", line 154, in get_preparation_data _check_not_importing_main() File "C:\Users\Ali.Khankan\AppData\Roaming\Python\Python39\site-packages\multiprocess\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.

setting multiprocessing_processes = 0 solved the problem

yglpyn8888 commented 2 years ago

i met this problem before and i comment out this directly ( # multiprocessing_processes=2)

sadimoodi commented 2 years ago

@yglpyn8888 i did this too, but then whats the point of using multi processing?

yglpyn8888 commented 2 years ago

@yglpyn8888 i did this too, but then whats the point of using multi processing?

This may have to be answered by the author. At this stage, I just want to run through the environment and test some questions that I care about.

AminHP commented 2 years ago

You should put your code inside this if __name__ == '__main__': statement if you are using multiprocessing. It's required because of the concept of forking in the OS kernel in order to handle the child processes, but we don't have to worry about that. Just put your code inside the mentioned if statement.