Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python多处理不退出_Python_Parallel Processing_Python 2.x - Fatal编程技术网

Python多处理不退出

Python多处理不退出,python,parallel-processing,python-2.x,Python,Parallel Processing,Python 2.x,我有一个代码正在成功运行,但运行时间太长。所以我决定尝试将其并行化 以下是代码的简化版本: import multiprocessing as mp import os import time output = mp.Queue() def calcSum(Nstart,Nstop,output): pid = os.getpid() for s in range(Nstart, Nstop): file_name = 'model' + str(s) +

我有一个代码正在成功运行,但运行时间太长。所以我决定尝试将其并行化

以下是代码的简化版本:

import multiprocessing as mp
import os
import time


output = mp.Queue()

def calcSum(Nstart,Nstop,output):
    pid = os.getpid()

    for s in range(Nstart, Nstop):
        file_name = 'model' + str(s) + '.pdb'

        file = 'modelMap' + str(pid) + '.dat'

        #does something with the contents of the pdb file
        #creates another file by using some other library:
        someVar.someFunc(file_name=file)

        #uses a function to read the file
        density += readFile(file)

        os.remove(file)

        print pid,s

    output.put(density)

if __name__ == '__main__':
    snapshots = int(sys.argv[1])
    cpuNum = int(sys.argv[2])

    rangeSet = np.zeros((cpuNum)) + snapshots//cpuNum
    for i in range(snapshots%cpuNum):
        rangeSet[i] +=1

    processes = []
    for c in range(cpuNum):
        na,nb = (np.sum(rangeSet[:c])+1, np.sum(rangeSet[:c+1]))
        processes.append(mp.Process(target=calcSum,args=(int(na),int(nb),output)))

    for p in processes:
        p.start()

    print 'now i''m here' 

    results = [output.get() for p in processes]

    print 'now i''m there' 

    for p in processes:
        p.join()

    print 'think i''l stay around'
    t1 =time.time()
    print len(results)
    print (t1-t0)
我使用命令
python run.py 10 4
运行这段代码

此代码在
calcSum
的外部循环中成功打印
pid
s
。我还可以看到终端中有两个CPU是100%的。所发生的情况是,最后打印出
pid 5
pid 10
,然后CPU使用率降至零,什么也没有发生。以下
print
语句都不起作用,脚本看起来仍然在终端中运行。我猜进程没有退出。是这样吗?我怎样才能修好它

以下是完整的输出:

$ python run.py 10 4
now im here
9600
9601
9602
9603
9602 7
9603 9
9601 4
9600 1
now im there
9602 8
9600 2
9601 5
9603 10
9600 3
9601 6
此时,我必须停止使用
Ctrl+C
终止

其他一些注意事项:

  • 如果我将os.remove(file)注释掉,我可以在目录中看到创建的文件
  • 不幸的是,在
    calcSum

EDIT起初,它可以切换
output.get()
p.join()
,但在代码中进行其他编辑后,这就不再起作用了。我已经更新了上面的代码。

apply join()对它们,我会guess@SeverinPappadeux我想我已经是了。请参阅下面的几行
在代码中打印“now i'm here”
。啊哈,很抱歉没有打印出来。输出后不应该调用join()。get()?写一个答案怎么样?我会支持的