title: For loop, trigger next / previous item from the outside function
body: <p>I just got my hands on Python (Flask) for the first time. I'm trying to build a simple API which will act as a remote playlist with <strong>Play / Pause / Stop  / Prev / Next features.</strong></p>
  
  <p>I've managed to build the first two features (Play / Stop), how ever I need some ideas for (Prev / Next). Bare in mind that at the end of every iteration, I have a sleep event. I'm using multithreading (Auto enabled by Flask) to be able to call <strong>stop()</strong> while <strong>play()</strong> is running.</p>
  
  <pre><code>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...'
  </code></pre>
  
  <p>I've tried creating the global 'currentIndex' variable which would keep track of the last played index in an array, and then use <a href="http://docs.python.org/2/library/collections.html#collections.deque" rel="nofollow noreferrer">deque</a> to rotate the array and start from the position where it was paused. This could work, but then again I still have Prev / Next features left</p>
  
  <p>I believe that there's a better way than using while and for to handle this, because from what I see it might not even be possible to use this approach and have (Prev / Next) features. </p>