title: Don't Run Async Function if Lock is Locked - Python
body: <p>Suppose that the function takes_time is called every 0.1 seconds on the arguments 0, 1, 2, 3, ...</p>
  <pre class="lang-py prettyprint-override"><code>async def takes_time(i):
      time.sleep(1)
  </code></pre>
  <p>After the first second, takes_time(0) will have been run once and takes_time(1), takes_time(2), ..., takes_time(9) are waiting to occur. I want to change this behavior so that those 9 calls are ignored, and takes_time only runs on the arguments 0, 10, 20, 30, etc. It does not matter to me whether they are off by 1 or 2 - I don't care about exactly which numbers are chosen - the main thing is that I want calls to takes_time to be ignored when another call is still running.</p>
  <p>Here is my solution:</p>
  <pre class="lang-py prettyprint-override"><code>lock = asyncio.Lock()
  async def takes_time(i):
      if lock.locked():
          return
      async with lock:
          time.sleep(1)
  </code></pre>
  <p>This solution works partially. It ensures that only one call to takes_time is running at a time, but it does not skip arguments 1 through 9 - even though the lock is locked when they are called. Why does this happen, and how can I fix it?</p>