【问题标题】:Why is training this xgboost model in a subprocess not terminating?为什么在子进程中训练这个 xgboost 模型没有终止?
【发布时间】:2022-10-08 05:25:23
【问题描述】:

鉴于以下程序在使用 run_process_timeout_wrapper 的子进程中运行 my_function 会导致超时(超过 160 秒),而“正常”运行它需要不到一秒的时间。

from multiprocessing import Process, Queue
import time
import numpy as np
import xgboost


def run_process_timeout_wrapper(function, args, timeout):

    def foo(n, out_q):
        res = function(*n)
        out_q.put(res)  # to get result back from thread target

    result_q = Queue()
    p = Process(target=foo, args=(args, result_q))
    p.start()

    try:
        x = result_q.get(timeout=timeout)
    except Empty as e:
        p.terminate()
        raise multiprocessing.TimeoutError("Timed out after waiting for {}s".format(timeout))

    p.terminate()
    return x


def my_function(fun):
    print("Started")
    t1 = time.time()
    pol = xgboost.XGBRegressor()
    pol.fit(np.random.rand(5,1500), np.random.rand(50,1))
    print("Took ", time.time() - t1)
    pol.predict(np.random.rand(2,1500))

    return 5


if __name__ == '__main__':

    t1 = time.time()
    pol = xgboost.XGBRegressor()
    pol.fit(np.random.rand(50,150000), np.random.rand(50,1))
    print("Took ", time.time() - t1)

    my_function(None)


    t1 = time.time()
    res = run_process_timeout_wrapper(my_function, (None,),160)
    
    print("Res ",  res, " Time ", time.time() - t1)

我在 Linux 上运行它。既然上来了,我还在my_function开头加了个print,说明至少达到了这个功能。

【问题讨论】:

  • 操作系统?您的工作进程不应在 Windows 上启动。
  • 我在 Linux/Manjaro 上运行

标签: python multiprocessing deadlock xgboost


【解决方案1】:

this 问题收集,我发现分叉多线程应用程序是problematic。一种可能的解决方案是添加

if __name__ == "__main__":
     mp.set_start_method('spawn')

但是,这可能会导致其他问题。

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多