title: How to use asyncio with existing blocking library?
body: <p>I have few blocking functions <code>foo</code>, <code>bar</code> and I can't change those (Some internal library I don't control. Talks to one or more network services). How do I use it as async?. E.g. I wan't to do the following.</p>
  
  <pre><code>results = []
  for inp in inps:
      val = foo(inp)
      result = bar(val)
      results.append(result)
  </code></pre>
  
  <p>This will be inefficient as I can call <code>foo</code> for the second input while I am waiting for the first and same for <code>bar</code>. How do I wrap them such that they are usable with asyncio (i.e new <code>async</code>, <code>await</code> syntax)?</p>
  
  <p>Lets assume the functions are re-entrant. i.e it is fine to call <code>foo</code> again when already a previous <code>foo</code> is processing.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>Extending answer with reusable decorator. Click <a href="https://stackoverflow.com/a/53719009/">here</a> for example.</p>
  
  <pre><code>def run_in_executor(f):
      @functools.wraps(f)
      def inner(*args, **kwargs):
          loop = asyncio.get_running_loop()
          return loop.run_in_executor(None, functools.partial(f, *args, **kwargs))
  
      return inner
  </code></pre>