title: How can I prevent the same task executed one OVER another?
body: <p>I have a task that pulled data from API and then do system automation (deleting, renaming, ...) with respect to a database (SQLite). I set the task to be executed every 5 minutes. However, sometimes the task takes more than 5 minutes to complete, hence two task run in parallel. This is not good because SQLite database is locked to the task in 0 minute.</p>
  
  <p>How can I have, either,</p>
  
  <ul>
  <li>the task will not be executed unless the previous task has already finished or</li>
  <li>the second task queued-up and executed directly after the 0 minute task finished?</li>
  </ul>
  
  <p>I have tried to use global boolean to prevent the task to be executed when it is running, like this.</p>
  
  <pre><code>automate_is_running = False
  @periodic_task(run_every=timedelta(minutes=5))
  def automate():
      if not automate_is_running:
          automate_is_running = True
  
          automate_all()
  
          automate_is_running = False
  </code></pre>
  
  <p>But that returns <code>UnboundLocalError: local variable 'automate_is_running' referenced before assignment</code> error. What should I do?</p>