laixintao / laixintao/python-parallel-programming-cookbook-cn

chapter3.5 如何杀掉一个进程

Open
#88 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
1.5k
Forks
97
PR merge metrics
No merged PRs in 30d

Description

文中说到terminate会立即杀死进程,但是输出如下下方的图,在p.terminate()之后,输出进程p.is_alive()返回了True,也就是进程还活着,但是在p.join()之后,再次输出p进程的状态却死了,经过自己尝试,terminate确实会立即去杀死进程,但是在主进程执行到p.is_alive()的时候p尚未被杀死所以导致了返回了True。看文章的时候我以为是和后面的join有关,后面尝试在terminate之后执行time.sleep(),发现与join并无关联。具体代码如下:

import multiprocessing
from multiprocessing.dummy import Process
import time

def foo():
    print("Starting func")
    time.sleep(0.1)

if __name__ == "__main__":
    p  = multiprocessing.Process(target=foo)
    print("Process before excution:", p, p.is_alive())
    p.start()
    print("Process running :", p.is_alive())
    p.terminate()
    time.sleep(0.1) # 如果没有这一行,下面一行的代码可能会返回True
    print("Process terminated:" , p.is_alive()) # process is alive
    p.join()
    print("Process joined:", p.is_alive()) # process is dead

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the chapter 3.5 passage about multiprocessing.Process. Compare its explanation with the Python example in the issue, especially the timing of terminate(), is_alive(), and join(). The documentation is done when it accurately explains the observed state changes and no longer suggests that join() causes termination.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.