对于循环,从外部函数触发下一个/上一个项目

时间:2019-01-10 14:11:42

标签: python python-3.x flask

我是第一次接触Python(Flask)。我正在尝试构建一个简单的API,该API具有播放/暂停/停止/上一个/下一个功能。

我已经设法构建了前两个功能(播放/停止),无论如何我需要一些想法(上/下一个)。切记,在每次迭代结束时,我都有一个睡眠事件。我正在使用多线程(由Flask自动启用),以便在 play()运行时调用 stop()

playing = 0


@app.route('/play', methods=['POST'])
def play():
    mediaFiles = request.json
    global playing
    playing = 1
    while playing == 1:
        for file in mediaFiles['mediaFiles']:
            if playing == 0:
                break
            print(file['path'])
            time.sleep(5)
    return 'Played'


@app.route('/stop')
def stop():
    global playing
    playing = 0
    return 'Stopped...'

我尝试创建全局'currentIndex'变量,该变量将跟踪数组中最后播放的索引,然后使用deque旋转数组并从其暂停的位置开始。这可能有效,但是再说一次,我仍然保留上一个/下一个功能

我认为,有一种比使用while和for更好的方法,因为从我看来,甚至可能无法使用这种方法并具有(Prev / Next)功能。

1 个答案:

答案 0 :(得分:1)

您需要做的就是保留文件的当前索引。但只是一个提示,但可能会或多或少:

playing = 0

current_index = 0


@app.route('/play', methods=['POST'])
def play():
    mediaFiles = request.json
    global playing
    playing = 1
    while playing == 1:
        for current_index, file in enumerate(mediaFiles['mediaFiles'][current_index:], current_index):
            if playing == 0:
                break
            print(file['path'])
            time.sleep(5)
        current_index = 0            # loop back to 0 after last file
    return 'Played'

上一个/下一个功能仅需增加/减少current_index。困难的部分是,如果您尝试从不同的线程更新current_index,则必须构建一个同步的迭代器以防止出现竞争情况。

例如,您可以使用:

class syncenumerate:
    def __init__(self, lock, iterable, start=0):
        self.lock = lock
        self.iter = enumerate(iterable, start)
    def __iter__(self):
        return self
    def __next__(self):
        self.lock.acquire()
        try:
            ret = next(self.iter)
        finally:
            self.lock.release()    # ensure that the lock is released at StopIteration
        return ret

您可以轻松获得线程安全同步:

playing=0
lck = threading.Lock()
current_changed = 0
...
    while playing == 1:
        for current_index, file in syncenumerate(lck, mediaFiles['mediaFiles']
                [current_index:], current_index):
            if current_changed = 1:    # restart enumeration is current was changed
                lck.acquire()
                current_changed = 0
                lck.release()
                break
            if playing == 0:
                ...

并在下一步中进行更改:

lck.acquire()
current_index += 1
if current_index >= max: current_index = 0
current_changed = 1
lck.release()