gpt4 book ai didi

python - 在 Python 中将 click.progressbar 与多处理一起使用

转载 作者:太空狗 更新时间:2023-10-29 21:34:02 25 4
gpt4 key购买 nike

我有一个巨大的列表需要处理,这需要一些时间,所以我将它分成 4 个部分,并用一些函数对每个部分进行多处理。使用 4 个内核运行仍然需要一些时间,所以我想我应该在函数中添加一些进度条,以便它可以告诉我每个处理器在处理列表时所处的位置。

我的梦想是拥有这样的东西:

erasing close atoms, cpu0  [######..............................]  13%
erasing close atoms, cpu1 [#######.............................] 15%
erasing close atoms, cpu2 [######..............................] 13%
erasing close atoms, cpu3 [######..............................] 14%

随着函数循环的进行,每个条都在移动。但相反,我得到了连续不断的流动:

enter image description here

等等,填充我的终端窗口。

这是调用该函数的主要 python 脚本:

from eraseCloseAtoms import *
from readPDB import *
import multiprocessing as mp
from vectorCalc import *

prot, cell = readPDB('file')
atoms = vectorCalc(cell)

output = mp.Queue()

# setup mp to erase grid atoms that are too close to the protein (dmin = 2.5A)
cpuNum = 4
tasks = len(atoms)
rangeSet = [tasks / cpuNum for i in range(cpuNum)]
for i in range(tasks % cpuNum):
rangeSet[i] += 1

rangeSet = np.array(rangeSet)

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

for p in processes:
p.start()

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

for p in processes:
p.join()

atomsNew = results[0] + results[1] + results[2] + results[3]

下面是函数eraseCloseAtoms():

import numpy as np
import click


def eraseCloseAtoms(protein, atoms, cell, spacing=2, dmin=1.4, output=None):
print 'just need to erase close atoms'

if dmin > spacing:
print 'the spacing needs to be larger than dmin'
return

grid = [int(cell[0] / spacing), int(cell[1] / spacing), int(cell[2] / spacing)]

selected = list(atoms)
with click.progressbar(length=len(atoms), label='erasing close atoms') as bar:
for i, atom in enumerate(atoms):
bar.update(i)
erased = False
coord = np.array(atom[6])

for ix in [-1, 0, 1]:
if erased:
break
for iy in [-1, 0, 1]:
if erased:
break
for iz in [-1, 0, 1]:
if erased:
break
for j in protein:
protCoord = np.array(protein[int(j)][6])
trueDist = getMinDist(protCoord, coord, cell, vectors)
if trueDist <= dmin:
selected.remove(atom)
erased = True
break
if output is None:
return selected
else:
output.put(selected)

最佳答案

接受的答案说点击是不可能的,它需要“非常多的代码才能使其工作”。

虽然这是真的,但还有另一个模块具有开箱即用的此功能:tqdm https://github.com/tqdm/tqdm这正是你所需要的。

您可以在文档中做嵌套进度条 https://github.com/tqdm/tqdm#nested-progress-bars等等

关于python - 在 Python 中将 click.progressbar 与多处理一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32080382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com