You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Multiprocessing Pool在Ubuntu上运行卡顿,Windows正常的问题求助

Python多进程Pool在Ubuntu下首次迭代后卡住问题

问题现象

  • Windows系统中脚本运行正常,CPU利用率达预期100%,串行版本执行无异常
  • 部署到Ubuntu系统后,Multiprocessing Pool完成第一次进程迭代后卡住,CPU利用率处于99.5%的空闲状态

已尝试方案

  • 设置进程启动方法为spawn:
    mp.set_start_method("spawn")
    
  • 配置maxtasksperchild=1参数

次要问题

日志模块在Windows和Ubuntu系统下均无法正常工作

启动代码

if __name__ == '__main__':
    mp.set_start_method("spawn")
    logging.basicConfig(filename='seasonality.log', level=logging.DEBUG)

    EOD_Hist = eod_historical.PullEOD(apikey, 'json')

    ticker = EOD_Hist.get_index_components_list(index)

    def main(ticker_):
        start_year = 1970
        start = '2023-01-01'
        end = '2023-12-31'
        logging.debug(f'{datetime.now()}: {ticker_} FULL TASKS STARTED - params:  start_year = {start_year} / start = {start} / end = {end}')
        hist, iter_range = raw_data(start_year, ticker_)
        start_df, end_df, performance_df, average_perf_df, median_perf_df, perf_stdv, max_draw_down_df, hit_ratio = period_iter(start, end, hist, iter_range)
        df = create_df(start_df, end_df, performance_df, average_perf_df, median_perf_df, perf_stdv, max_draw_down_df, hit_ratio)
        helper_functions.write_df_to_database(mongoip, mongouser, mongopw, mongotable, ticker, df)
        logging.debug(f'{datetime.now()}: {ticker_} FULL TASKS ENDED - length_df_written = {df}')
        return f'{ticker} done'

    pool = mp.Pool(os.cpu_count(), maxtasksperchild=1)                         # Create a multiprocessing Pool
    result = pool.map(main, ticker)
    print(result)

补充说明

被调用的函数运行正常,串行版本可正常执行,且API调用的每日额度在正常减少(说明API请求逻辑无问题)

内容的提问来源于stack exchange,提问作者Lorenz Weidinger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:02:53