-1

它正确删除了第一个循环中的所有内容,但是在删除第二个循环中的快照时抛出了这个问题,知道为什么吗?我可能一直在运行 python 3,现在又回到了 2.7,这可能是我的问题吗?它似乎从列表中删除了一项,然后无法再删除。

for i in range(0, numOfSnap):
            tempName = baseVv.name + ".ro" + str(i)
            snapVv = baseVv.createSv(name=baseVv.name + ".ro" + str(i), svOptions="-ro -i %s" % x)  
            x += 1                                      
            dlog.info("Creating %s" % tempName)
            baseVv.name + ".rw" + str(i)
            tempName = baseVv.name + ".rw" + str(i)
            rwSnap = snapVv.createSv(name=baseVv.name + ".rw" + str(i), svOptions="-i %s" % x)
            x += 1 
            snapRwVlun = self.cluster.createVlun(vv=rwSnap, host=host)
            dlog.info("Creating %s" % tempName)
            rwSnapList.append(rwSnap)
            vlunList.append(snapRwVlun)
            ## delay random seconds between RO snapshot creation
            time.sleep(random.randint(5,30))

        snapRoList = baseVv.snapSvList

        dlog.info("Stopping IO on VV")
        self.cluster.stopSnapIOOnHosts()

        dlog.info("Starting IO on snaps and VV")           
        self.cluster.startSnapIOOnHosts()
        dlog.info("Sleep 60 seconds for IO to go through")

        time.sleep(60)

        dlog.info("Stopping IO")
        self.cluster.stopSnapIOOnHosts()                       

        time.sleep(60)

        for vluns in vlunList:
            vluns.remove()

        dlog.info("Waiting 60 seconds")    
        time.sleep(60)

        ## randomly pick 1 rw snapshot to delete till removal of all snapshot
        for i in range(0, numOfSnap)[:]:
            snapIndex = random.randint(0, len(rwSnapList)-1)
            dlog.info("remove %s" % rwSnapList[snapIndex].name)
            roSnapshot =  rwSnapList[snapIndex]
            roSnapshot.remove()
            #self.cluster.waitForClusterIntegrated()
            ## remove it from object list
            rwSnapList.remove(roSnapshot)
            time.sleep(random.randint(5,10))

        ## randomly pick 1 ro snapshot to delete till removal of all snapshot
        for i in range(0, numOfSnap)[:]:
            snapIndex = random.randint(0, len(snapRoList)-1)
            dlog.info("remove %s" % snapRoList[snapIndex].name)
            roSnapshot =  snapRoList[snapIndex]
            roSnapshot.remove()
            #self.cluster.waitForClusterIntegrated()
            ## remove it from object list
            snapRoList.remove(roSnapshot)
            time.sleep(random.randint(5,10))
4

2 回答 2

2

与其从快照列表中选择随机索引,不如将列表洗牌一次并从该洗牌列表中弹出项目:

shuffledSnapList = snapRoList[:]
random.shuffle(shuffledSnapList)

while shuffledSnapList:
    roSnapshot = shuffledSnapList.pop()

通过while对要从中删除的列表进行循环测试,可以保证当列表为空时循环将终止。

因为列表是随机排列的,所以项目是按随机顺序处理的。

于 2013-08-15T09:32:22.690 回答
0

你为什么要首先从列表中删除它们,你只想调用.remove()所有机器?

def removeAll(snapshots, minsleep=5, maxsleep=10):
    random.shuffle(snapshots)
    for snapshot in snapshots:
        dlg.info("remove %s" % snapshot.name)
        snapshot.remove()
        time.sleep(random.randint(minsleep, maxsleep))

removeAll(rwSnapList)
removeAll(roSnapList)
于 2013-08-15T11:50:46.050 回答