gpt4 book ai didi

python - 如何查看已完成或剩余的 map_async 作业数?

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:17 25 4
gpt4 key购买 nike

我使用 iPython 的并行处理工具进行大 map 操作。在等待 map 操作完成时,我想向用户显示有多少作业已完成,有多少正在运行,还有多少剩余。我怎样才能找到这些信息?

这是我的工作。我创建了一个使用本地引擎的配置文件并启动了两个工作人员。在外壳中:

$ ipython profile create --parallel --profile=local
$ ipcluster start --n=2 --profile=local

这是客户端 Python 脚本:

#!/usr/bin/env python

def meat(i):
import numpy as np
import time
import sys
seconds = np.random.randint(2, 15)
time.sleep(seconds)
return seconds

import time
from IPython.parallel import Client

c = Client(profile='local')
dview = c[:]

ar = dview.map_async(meat, range(4))
elapsed = 0
while True:
print 'After %d s: %d running' % (elapsed, len(c.outstanding))
if ar.ready():
break
time.sleep(1)
elapsed += 1
print ar.get()

脚本输出示例:

After 0 s: 2 running
After 1 s: 2 running
After 2 s: 2 running
After 3 s: 2 running
After 4 s: 2 running
After 5 s: 2 running
After 6 s: 2 running
After 7 s: 2 running
After 8 s: 2 running
After 9 s: 2 running
After 10 s: 2 running
After 11 s: 2 running
After 12 s: 2 running
After 13 s: 2 running
After 14 s: 1 running
After 15 s: 1 running
After 16 s: 1 running
After 17 s: 1 running
After 18 s: 1 running
After 19 s: 1 running
After 20 s: 1 running
After 21 s: 1 running
After 22 s: 1 running
After 23 s: 1 running
[9, 14, 10, 3]

如您所见,我可以获得当前正在运行的作业数,但无法获得已完成(或剩余)的作业数。我如何知道有多少 map_async 的作业已完成?

最佳答案

AsyncResult 有一个 msg_ids 属性。 outstanding jobs是和rc.outstanding的交集,完成的jobs是差值:

msgset = set(ar.msg_ids)
completed = msgset.difference(rc.outstanding)
pending = msgset.intersection(rc.outstanding)

关于python - 如何查看已完成或剩余的 map_async 作业数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7655825/

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