简体   繁体   English

Python Multiprocess不会终止

[英]Python Multiprocess not terminate

I am new to python multiprocess and I want to understand why my code does not terminate (maybe zombi or deadlock) and how to fix it. 我是python multiprocess的新手,我想了解为什么我的代码不会终止(可能是zombi或死锁)以及如何修复它。 The createChain functions execute a for loop also and returns a tuple: (value1, value2) . createChain函数也执行for循环并返回一个元组: (value1, value2) Inside createChain function there are other calls to other functions. createChain函数内部还有其他函数调用。 I don't think posting the createChain function code will help because inside that function I am not doing something regarding multiprocess. 我不认为发布createChain函数代码会有所帮助,因为在该函数内部我没有做createChain事情。 I tried to make the processes as deamon but still didn't work. 我试图让这些过程成为deamon,但仍然无法正常工作。 The strange think is that if I decrease the value of maxChains ie to 500 or 100 is working. 奇怪的是,如果我减小maxChains的值,即500或100正在工作。

I just want the process to do some heavy tasks and put the results to a data type. 我只是希望该过程执行一些繁重的任务并将结果放入数据类型。

My version of python is 2.7 我的python版本是2.7

def createTable(chainsPerCore, q, chainLength):

    for chain in xrange(chainsPerCore):
         q.put(createChain(chainLength, chain))


def initTable():
    maxChains = 1000
    chainLength = 10000
    resultsQueue = JoinableQueue()
    numOfCores = cpu_count()
    chainsPerCore = maxChains / numOfCores

    processes = [Process(target=createTable, args=(chainsPerCore, resultsQueue, chainLength,)) for x in range(numOfCores)]

    for p in processes:
        # p.daemon = True
        p.start()

    # Wait for hashing cores to finish
    for p in processes:
        p.join()

    resultsQueue.task_done()

    temp = [resultsQueue.get() for p in processes]
    print temp

Based on the very useful comments of Tadhg McDonald-Jensen I understood better my needs and how the Queues are workings and for what purpose they should be used. 根据Tadhg McDonald-Jensen非常有用的评论,我更了解我的需求以及队列的运作方式以及它们应该用于何种目的。

I change my code to 我将代码更改为

def initTable(output):
    maxChains = 1000

    results = []

    with closing(Pool(processes=8)) as pool:
        results = pool.map(createChain, xrange(maxChains))
        pool.terminate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM