让 for 循环在后台工作 while 循环

Making for loop working in the background while loop

所以我在这里很困惑

while self.voice_client.is_playing() == True:
   self.time += 1
    self.spot.clear()
for item in self.spot:
   video = search(query=item)
   self.query.append(video)

如何将这两部分结合起来?我希望 for loopwhile loop 同时工作。虽然 self.item += 1,但 for item... 也是 运行

让 for 循环在后台运行 while 循环

通过对 for 循环的功能进行编程,将 for 循环的功能置于 while 循环中。

A for 循环处理一个可迭代对象,直到项目结束。项目的末尾由 StopIteration 错误标识,该错误导致退出 for 循环。

要复制代码中的功能,请确保忽略 StopIteration 并继续外循环。

# Build for loop functionality
self_spot = ['Song_1','Song_2']
self_spot_items = iter(self_spot) # The iterable part of a for loop

count = 0 #
while True:
    count += 1
    # For loop functionality
    try:
        print(f'{next(self_spot_items)=}')
        # video = search(query=item)
        # self.query.append(video)
    except StopIteration: # item processing done
        pass # continue the while loop
    # Outer loop functionlaity
    print(f'{count=}')
    if count > 10:
        break

输出

next(self_spot_items)='Song_1'
count=1
next(self_spot_items)='Song_2'
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
count=11