在 pandas-ta 库中使用多处理

using multiproccesing with the pandas-ta library

我正在尝试使用技术分析库 pandas-ta 的多处理功能。 该示例在此处描述 https://pythonrepo.com/repo/twopirllc-pandas-ta-python-deep-learning#multiprocessing

但是我不能让这个例子太有效,错误信息给出了一个明确的提示,说明可能是什么地方出了问题“已尝试在 当前进程已完成其引导阶段。"

我需要帮助破译错误消息,我在这个例子中做错了什么?


df1 = pd.DataFrame() # Empty DataFrame

# Load data
df1 = pd.read_csv("I Provide data further down.csv", sep=",")

def cleanUp(frame):
    frame = frame.iloc[:, :6]
    frame.columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
    frame[['Open', 'High', 'Low', 'Close', 'Volume']] = frame[['Open', 'High', 'Low', 'Close', 'Volume']].astype(float)
    #frame.Time = pd.to_datetime(frame.Time, unit='ms')
    return frame

df = cleanUp(df1)

df.set_index(pd.DatetimeIndex(df["datetime"]), inplace=True)

df.ta.strategy()

df.ta.strategy(verbose=True)

df.ta.strategy(timed=True)

# Choose the number of cores to use. Default is all available cores.
# For no multiprocessing, set this value to 0.
df.ta.cores = 4

print(df.columns)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\jeppe\PycharmProjects\pandas-ta-trial\main.py", line 47, in <module>
    df.ta.strategy()
  File "C:\Users\jeppe\PycharmProjects\test\pandas-ta-trial\lib\site-packages\pandas_ta\core.py", line 725, in strategy
    with Pool(self.cores) as pool:
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 119, in Pool
    return Pool(processes, initializer, initargs, maxtasksperchild,
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\pool.py", line 212, in __init__
    self._repopulate_pool()
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\pool.py", line 303, in _repopulate_pool
    return self._repopulate_pool_static(self._ctx, self.Process,
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\pool.py", line 326, in _repopulate_pool_static
    w.start()
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\jeppe\AppData\Local\Programs\Python\Python39\lib\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.
1577836800.0,7195.24000000,7196.25000000,7178.64000000,7179.78000000,95.50913300,1577837099999,686317.13625177,1127,32.77324500,235537.29504531,0
1577837100.0,7179.76000000,7191.77000000,7178.20000000,7191.07000000,59.36522500,1577837399999,426481.26036406,631,24.76651300,177935.61820100,0
1577837400.0,7193.15000000,7193.53000000,7180.24000000,7180.97000000,48.06851000,1577837699999,345446.50301879,694,19.42228300,139596.62168263,0
1577837700.0,7180.97000000,7186.40000000,7177.35000000,7178.29000000,32.19292900,1577837999999,231162.55542356,576,12.96325800,93091.43327629,0
1577838000.0,7177.71000000,7182.46000000,7175.47000000,7176.96000000,49.02739700,1577838299999,351927.89388145,710,22.81974400,163817.88115474,0
1577838300.0,7177.59000000,7185.56000000,7176.11000000,7178.45000000,47.02232800,1577838599999,337612.29777385,662,22.60610800,162317.15701592,0
1577838600.0,7178.19000000,7185.44000000,7177.54000000,7180.68000000,35.10983000,1577838899999,252130.90500262,635,19.13954100,137447.70846574,0
1577838900.0,7180.96000000,7182.53000000,7176.23000000,7177.53000000,24.86349600,1577839199999,178533.13638991,515,10.40067900,74689.13949208,0
1577839200.0,7177.14000000,7182.45000000,7176.34000000,7179.56000000,23.51413200,1577839499999,168815.11141275,430,14.28215400,102530.47311368,0
1577839500.0,7179.35000000,7182.99000000,7179.35000000,7182.94000000,21.35885000,1577839799999,153387.48781809,388,13.23975300,95080.11545929,0
1577839800.0,7182.94000000,7183.98000000,7175.51000000,7179.03000000,42.91097100,1577840099999,308092.13673214,739,20.42254800,146652.86619733,0
1577840100.0,7178.65000000,7181.75000000,7175.46000000,7177.02000000,32.87210000,1577840399999,235950.15541644,533,13.31730000,95592.88054636,0
1577840400.0,7176.47000000,7185.86000000,7175.71000000,7183.29000000,30.16307600,1577840699999,216611.56911648,596,13.60798500,97723.51606860,0
1577840700.0,7183.55000000,7194.04000000,7182.82000000,7189.62000000,44.80332700,1577840999999,322109.95174577,564,31.19321800,224247.23205552,0
1577841000.0,7189.63000000,7194.04000000,7188.62000000,7190.86000000,28.55411900,1577841299999,205367.58317887,428,11.45017500,82359.75026194,0
1577841300.0,7190.46000000,7194.27000000,7189.23000000,7194.06000000,24.21607800,1577841599999,174147.67835753,449,9.21024500,66240.18705864,0
1577841600.0,7193.02000000,7198.00000000,7190.79000000,7192.39000000,34.10757500,1577841899999,245382.36092326,546,15.49876300,111511.59548775,0
1577841900.0,7193.01000000,7217.00000000,7191.98000000,7212.10000000,193.21939900,1577842199999,1392268.35865829,1477,141.44239000,1019053.48118736,0
1577842200.0,7212.10000000,7230.00000000,7211.32000000,7218.83000000,273.46807000,1577842499999,1974637.04622372,1837,204.41950300,1475997.47530984,0
1577842500.0,7219.11000000,7221.95000000,7210.53000000,7216.00000000,60.97776600,1577842799999,440073.27302928,696,25.91291800,187028.64162685,0
1577842800.0,7216.02000000,7219.11000000,7207.17000000,7211.87000000,52.80141600,1577843099999,380871.05492710,670,27.73147800,200051.89521924,0
1577843100.0,7212.34000000,7212.67000000,7205.01000000,7205.08000000,47.98705500,1577843399999,345869.92301800,583,22.70245600,163640.51690998,0
1577843400.0,7205.08000000,7211.77000000,7204.28000000,7209.35000000,54.49179200,1577843699999,392739.25101900,668,43.09262500,310591.99690708,0
1577843700.0,7209.75000000,7216.41000000,7207.39000000,7216.27000000,38.26293000,1577843999999,275874.49091546,519,24.45966300,176357.76159813,0
1577844000.0,7215.52000000,7223.37000000,7214.09000000,7217.25000000,62.72749700,1577844299999,452754.08488518,809,34.32410800,247754.49464240,0
1577844300.0,7217.21000000,7223.80000000,7214.27000000,7217.18000000,40.37927000,1577844599999,291472.74386075,450,22.14185300,159841.97734924,0
1577844600.0,7217.73000000,7219.03000000,7211.41000000,7211.97000000,30.65388600,1577844899999,221159.98976269,440,11.44721300,82588.05896068,0
1577844900.0,7212.87000000,7225.00000000,7212.15000000,7224.20000000,32.41522200,1577845199999,234036.30960380,410,26.24226500,189467.38539295,0
1577845200.0,7224.20000000,7230.00000000,7218.97000000,7229.32000000,36.71092700,1577845499999,265247.94179368,540,15.18070600,109691.04940257,0
1577845500.0,7229.50000000,7238.88000000,7224.91000000,7228.09000000,106.13135500,1577845799999,767622.84737177,1031,45.92922600,332190.77953536,0
1577845800.0,7227.12000000,7236.07000000,7226.79000000,7234.76000000,56.68833600,1577846099999,409950.24832819,629,33.52172000,242416.94296405,0
1577846100.0,7233.83000000,7235.83000000,7228.08000000,7232.21000000,73.99718000,1577846399999,535146.93433720,660,41.72203500,301706.93949934,0
1577846400.0,7232.75000000,7237.99000000,7229.65000000,7237.63000000,42.63381900,1577846699999,308344.14964985,462,25.20989500,182352.19988289,0
1577846700.0,7237.42000000,7237.81000000,7231.93000000,7236.13000000,38.42991400,1577846999999,278030.89841814,517,17.19293100,124376.96447313,0
1577847000.0,7237.21000000,7244.20000000,7232.20000000,7243.65000000,89.94917000,1577847299999,651134.69258648,964,62.15063300,449941.31467835,0
1577847300.0,7243.07000000,7244.87000000,7238.00000000,7242.85000000,44.44023300,1577847599999,321818.54759365,554,22.11767800,160185.71830680,0
1577847600.0,7242.66000000,7245.00000000,7230.25000000,7235.33000000,87.25912300,1577847899999,631447.62459136,879,37.94202400,274571.31474658,0
1577847900.0,7235.18000000,7235.66000000,7220.10000000,7224.98000000,108.38500300,1577848199999,783396.65916757,817,32.88055900,237655.73210438,0
1577848200.0,7224.98000000,7229.70000000,7223.43000000,7228.50000000,75.37889900,1577848499999,544807.54913650,1115,58.98292400,426315.39696847,0
1577848500.0,7227.43000000,7235.50000000,7227.19000000,7235.50000000,76.26424900,1577848799999,551573.18631218,943,37.74926700,273025.08551912,0```

从堆栈跟踪来看,您的代码似乎在 Windows 下 运行,并且在 Windows 上使用多处理代码时,它必须由 if __name__ == "__main__":[= 保护12=]

将您的代码包装在 main() 函数中,然后在 top-level:

处调用它会更安全
if __name__ == "__main__":
    main()

错误描述中也给出了相同的建议。