title: Debugging python async race condition with defaultdict
body: <p>I'm trying to write something that collates a set of inputs that are sent within some time frame into a single request. My code is similar to:</p>
  
  <pre class="lang-py prettyprint-override"><code>in_dict = defaultdict(list)
  out_dict = defaultdict(list)
  guard = asyncio.Lock()
  
  async def collate(k, v):
      async with guard:
          in_dict[k].append(v)
      await asyncio.sleep(wait_time)
      async with guard:
          if k in in_dict and in_dict[k]:
              out_dict[k] = out_dict[k] + in_dict.pop(k)
          elif k in out_dict:
              handle_collated(out_dict.pop(k))
  </code></pre>
  
  <p>However, there seems to be a race condition somewhere where <code>out_dict[k]</code> still contains elements that have been processed.</p>
  
  <p>Thanks</p>