为什么并行作业的状态在作业完成后仍然没有设置为”finished”?

huangapple go评论54阅读模式
英文:

Why does the status of parallel jobs is never set to "finished" even after job finished?

问题

以下是您要求的翻译:

我使用下面的代码作为一个并行运行作业的示例。尽管从输出中我可以看到作业已经完成,但状态仍然为false。为什么会这样?我期望 "status_dict" 的状态在作业完成后会更改为 "True"。

但从下面的输出中,我们可以看到即使作业已经完成,状态仍然保持为False。

    ----------123
    {'JOB4': False, 'JOB11': False, 'JOB23': False}
    ----
    JOB4 False
    Starting job JOB11...
    Starting job JOB4...
    Starting job JOB23...
    ----
    JOB11 False
    ----
    JOB23 False
    ----
    JOB4 False
    Finished job JOB23.
    Finished job JOB4.
    Finished job JOB11.
    ----
    JOB11 False
    ----
    JOB23 False
    ----
    JOB4 False
    ----
    JOB11 False

代码:

import multiprocessing, time

# 并行运行作业
def run_job(jn, par):
    print(f"Starting job {jn}...")
    time.sleep(3)
    print(f"Finished job {jn}.")


if __name__ == '__main__':
    list_jobs = ["JOB4", "JOB11", "JOB23"]
    processes = []

    status_dict = multiprocessing.Manager().dict()
    for jn in list_jobs:
        par = {}
        par["sleep"] = 5
        status_dict[jn] = False
        p = multiprocessing.Process(target=run_job, args=(jn, par))
        processes.append(p)
        p.start()

    print("------------123")
    print(status_dict)
    while not all(status_dict.values()):
        for job_id, status in status_dict.items():
            print("----")
            print(job_id, status)
            time.sleep(1)

    print("ENDE")
英文:

I use below code as an example to run jobs in parallel. Even though I see from output that the Jobs have finished, the status is still false. Why is that? I would expect that the status of "status_dict" changes to "True" for finished jobs.

But from below output we see that status stays False even when job has finished.

----------123
{'JOB4': False, 'JOB11': False, 'JOB23': False}
----
JOB4 False
Starting job JOB11...
Starting job JOB4...
Starting job JOB23...
----
JOB11 False
----
JOB23 False
----
JOB4 False
Finished job JOB23.
Finished job JOB4.
Finished job JOB11.
----
JOB11 False
----
JOB23 False
----
JOB4 False
----
JOB11 False

Code:

import multiprocessing, time

# run jobs parallel
def run_job(jn, par):
    print(f"Starting job {jn}...")
    time.sleep(3)
    print(f"Finished job {jn}.")


if __name__ == '__main__':    
    list_jobs = ["JOB4", "JOB11", "JOB23"]
    processes = []

    status_dict = multiprocessing.Manager().dict()
    for jn in list_jobs:
        par = {}
        par["sleep"] = 5
        status_dict[jn] = False
        p = multiprocessing.Process(target=run_job, args=(jn, par))
        processes.append(p)
        p.start()

    
    print("------------123")
    print(status_dict)
    while not all(status_dict.values()):
        for job_id, status in status_dict.items():
            print("----")
            print(job_id, status)
            time.sleep(1)


    print("ENDE")

答案1

得分: 0

问题在于我没有更新状态。以下代码有效:

import multiprocessing, time

# 并行运行作业
def run_job(jn, status):
    print(f"Starting job {jn}...")
    time.sleep(3)
    status.value = True
    print(f"Finished job {jn}.")

if __name__ == '__main__':
    list_jobs = ["JOB4", "JOB11", "JOB23"]
    processes = []

    status_dict = {}
    for jn in list_jobs:
        par = {}
        par["sleep"] = 5
        status_dict[jn] = multiprocessing.Value('b', False)
        p = multiprocessing.Process(target=run_job, args=(jn, status_dict[jn]))
        processes.append(p)
        p.start()

    print("------------123")
    for i in range(100):
        print(status_dict)
        for k in status_dict:
            print("----")
            print(k, status_dict[k].value)
            time.sleep(1)

    print("ENDE")

如果您有其他问题,请随时提出。

英文:

Problem is I was not updating the status. Below code works:

import multiprocessing, time

# run jobs parallel
def run_job(jn, status):
    print(f"Starting job {jn}...")
    time.sleep(3)
    status.value = True
    print(f"Finished job {jn}.")


if __name__ == '__main__':    
    list_jobs = ["JOB4", "JOB11", "JOB23"]
    processes = []

    status_dict = {}
    for jn in list_jobs:
        par = {}
        par["sleep"] = 5
        status_dict[jn] = multiprocessing.Value('b', False)
        p = multiprocessing.Process(target=run_job, args=(jn, status_dict[jn]))
        processes.append(p)
        p.start()

    
    print("------------123")
    for i in range(100):
        print(status_dict)
        for k in status_dict:
            print("----")
            print(k, status_dict[k].value)
            time.sleep(1)


    print("ENDE")

答案2

得分: 0

status_dict 之所以对所有作业保持False状态,是因为你没有在run_job函数内更新字典。

要更新作业的状态,你可以将status_dict作为参数传递给run_job函数,并在作业完成时在函数内更新它。修改后的函数代码如下:

def run_job(jn, par, status_dict):
    print(f"Starting job {jn}...")
    time.sleep(3)
    print(f"Finished job {jn}.")
    status_dict[jn] = True

然后,在启动进程时,你可以将status_dict作为参数传递:

p = multiprocessing.Process(target=run_job, args=(jn, par, status_dict))

经过这些更改,代码应该可以正常运行了。

英文:

The reason why the status_dict remains False for all jobs is because you're not updating the dictionary within the run_job function.

To update the status of a job, you can pass the status_dict as an argument to the run_job function, and update it within the function whenever a job is finished. Modified code of function:

def run_job(jn, par, status_dict):
    print(f"Starting job {jn}...")
    time.sleep(3)
    print(f"Finished job {jn}.")
    status_dict[jn] = True

Then, when you start the processes, you can pass the status_dict as an argument:

p = multiprocessing.Process(target=run_job, args=(jn, par, status_dict))

After these changes the code should be fine and work as excepted.

huangapple
  • 本文由 发表于 2023年4月13日 16:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定