Python 多进程挂起

Python 多进程挂起,python,python-multiprocessing,Python,Python Multiprocessing,所以我开始尝试Python的多处理库。我的目标是加速一个缓慢的函数,该函数将一个字符串与其他字符串的大型数据库进行比较,并返回最相似的匹配。为此,我尝试编写一个函数,在不同的流程对象之间拆分任务并设置它们运行,使用共享变量捕获结果: cores = cpu_count() # Number of cores in this computer, i.e. 4 sublistList = chunks(tasks,cores) # Split tasks into subprocessing arr

所以我开始尝试Python的多处理库。我的目标是加速一个缓慢的函数,该函数将一个字符串与其他字符串的大型数据库进行比较,并返回最相似的匹配。为此,我尝试编写一个函数,在不同的流程对象之间拆分任务并设置它们运行,使用共享变量捕获结果:

cores = cpu_count() # Number of cores in this computer, i.e. 4
sublistList = chunks(tasks,cores) # Split tasks into subprocessing arrays of evenly-sized chunks, the number of chunks equal to how many cores we have to process them

# Create a multiprocessing function, since this is a large function that will take time and splitting it across cores will ease the load
if __name__ == '__main__':

    freeze_support() # Make sure multiple applications don't spawn, this is necessary for Windows

    jobs = [] # Array of processes
    manager = Manager() # Create a manager
    returns = manager.list() # Shared list variable we use to get return results

    for i in range(0,cores): # For number of cores...

        p = Process(target=workerFunction,args=(w,sublistList[i],returns))
        jobs.append(p) # Add to array of processes to run
        p.start()

    for p in jobs:

        p.join()
然而,当我运行这段代码时,它会创建一个新的应用程序窗口,然后无限期地挂起,这是完全奇怪的行为,根本不是我想要的。是什么导致我的代码出现这种情况?我的worker函数是否正在悄无声息地崩溃而没有提醒我?我看过很多其他的答案,但是没有一个建议的答案能够解决这个问题

(如果这与这个问题有关,我是一名入门级软件工程师,有几年其他语言的编程经验,但对Python比较陌生。这是我的一个独立游戏小项目。)

这还不是答案,但我发布它是为了向您展示一个runnable的示例

代码基于您当前的问题,加上使其可运行所缺少的所有其他内容。毫不奇怪,因为所有这些都只是猜测,它不会重现您所说的问题,但这可能是因为我的一个或多个猜测在某些重要方面有所不同……这就是为什么您真的应该是提供所有代码的人

一个观察结果是:最后的
p.join()
调用将使主进程等待每个子进程完成。这将导致主进程在等待每个进程时出现“挂起”

from multiprocessing import *
from time import sleep

tasks = None

def chunks(tasks, cores):
    return [[i for _ in range(8)] for i in range(cores)]

def workerFunction(w, sublist, returns):
    print('starting workerFunction:', w)
    result = [value+100 for value in sublist]
    returns.append(result)
    sleep(3)
    print('exiting workerFunction:', w)

if __name__ == '__main__':

    # Only do in main process.
    freeze_support()
    cores = cpu_count()
    sublistList = chunks(tasks, cores)
    manager = Manager()
    returns = manager.list()
    jobs = []

    for i in range(cores):
        w = i
        p = Process(target=workerFunction, args=(w, sublistList[i], returns))
        jobs.append(p)
        p.start()

    for i, p in enumerate(jobs, 1):
        print('joining job[{}]'.format(i))
        p.join()

    # Display results.
    for sublist in returns:
        print(sublist)

    print('done')
输出:

加入作业[1]
启动工作功能:2
启动workerFunction:1
正在启动workerFunction:0
启动工作功能:5
启动工作功能:7
启动工作功能:3
启动工作功能:4
启动工作功能:6
正在退出workerFunction:2
正在退出workerFunction:0
正在退出workerFunction:1
加入工作[2]
正在退出workerFunction:5
加入工作[3]
加入工作[4]
退出workerFunction:7
正在退出workerFunction:3
正在退出workerFunction:4
加入工作[5]
正在退出workerFunction:6
加入工作[6]
加入工作[7]
加入工作[8]
[102, 102, 102, 102, 102, 102, 102, 102]
[101, 101, 101, 101, 101, 101, 101, 101]
[100, 100, 100, 100, 100, 100, 100, 100]
[105, 105, 105, 105, 105, 105, 105, 105]
[107, 107, 107, 107, 107, 107, 107, 107]
[103, 103, 103, 103, 103, 103, 103, 103]
[104, 104, 104, 104, 104, 104, 104, 104]
[106, 106, 106, 106, 106, 106, 106, 106]
完成
按任意键继续。

您的问题中没有足够的代码供任何人回答。请检查并提供MCVE。请参阅。
join
方法以某种方式等待子流程完成。似乎您的工作程序正在无限运行。不应该是“工作程序函数无声崩溃”。如果子进程崩溃并退出,p.join()将返回。尝试在死循环或挂起状态下调试“工作函数”。任何使用
多进程的任务都不得挂起任何未超时的功能。另一方面,您还应该使用
try:except:
来捕获错误。根据需要,您可能需要使用异步处理。