如何正确终止multiprocessing.Process()?

How to correctly terminate multiprocessing.Process()?

我有一段代码需要以下列方式 运行 多个并行线程:

def driver():
    #Gets a UID and passes it to Run
    run(uid)

def f1(a,b,c):
    #Does Some thing : Trivial Example Below - These Operations might take long time
    d=a+b+c
    print(d)

def f2(a,b,c):
    #Does Some thing : Trivial Example Below - These Operations might take long time
    d=a+b*c
    e=a+b/c
    print(d,e)

现在,我有一个 run() 方法可以并行 运行 这些方法。

from multiprocessing import Process
def run(uid):
    #Get a,b,c on the basis of UID then call the following
    thread_1=Process(target=f1, args=(a,b,c))
    thread_2=Process(target=f2, args=(a,b,c))
    thread_1.start()
    thread_2.start()
    thread_1.join()
    thread_2.join()

代码的入口点是driver()。 然而,我的代码在一定数量的操作后卡住了,我猜这是由于内存问题。

我的问题是:如何在线程成功执行后正确终止线程?执行后是否还驻留在内存中?

我运行处理这些线程的方式有什么问题吗?

使用 Threadpool 执行器或线程库来更轻松地管理事情。

Threadpool执行器可以如下使用。

def test(n):
 print(n)
            
with ThreadPoolExecutor(max_workers=5) as executor:
  for i in range(10):
     executor.submit(test, args)

https://apscheduler.readthedocs.io/en/latest/userguide.html