Все, что вам нужно, это сохранить текущий индекс для файла. Но только намек, но это может быть более или менее:
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:
...
И изменить, например, в Next:
lck.acquire()
current_index += 1
if current_index >= max: current_index = 0
current_changed = 1
lck.release()