title: Python asyncio task got bad yield
body: <p>I am confused about how to play around with the <code>asyncio</code> module in Python 3.4. I have a <code>searching</code> API for a search engine, and want to each search request to be run either parallel, or asynchronously, so that I don't have to wait for one search finish to start another.</p>
  
  <p>Here is my high-level searching API to build some objects with the raw search results. The search engine itself is using some kind of asyncio mechanism, so I won't bother with that.</p>
  
  <pre><code># No asyncio module used here now
  class search(object):
    ...
    self.s = some_search_engine()
    ...
    def searching(self, *args, **kwargs):
      ret = {}
      # do some raw searching according to args and kwargs and build the wrapped results
      ...
      return ret
  </code></pre>
  
  <p>To try to async the requests, I wrote following test case to test how I can interact my stuff with the <code>asyncio</code> module.</p>
  
  <pre><code># Here is my testing script
  @asyncio.coroutine
  def handle(f, *args, **kwargs):
    r = yield from f(*args, **kwargs)
    return r
  
  s = search()
  loop = asyncio.get_event_loop()
  loop.run_until_complete(handle(s.searching, arg1, arg2, ...))
  loop.close()
  </code></pre>
  
  <p>By running with pytest, it will return a <code>RuntimeError: Task got bad yield : {results from searching...}</code>, when it hits the line <code>r = yield from ...</code>. </p>
  
  <p>I also tried another way.</p>
  
  <pre><code># same handle as above
  def handle(..):
    ....
  s = search()
  loop = asyncio.get_event_loop()
  tasks = [
          asyncio.async(handle(s.searching, arg11, arg12, ...)),
          asyncio.async(handle(s.searching, arg21, arg22, ...)),
          ...
          ]
  loop.run_until_complete(asyncio.wait(tasks))
  loop.close()
  </code></pre>
  
  <p>By running this test case by pytest, it passes but some weird exception from the search engine will raise. And it says <code>Future/Task exception was never retrieved</code>.</p>
  
  <p>Things I wish to ask:</p>
  
  <ol>
  <li>For my 1st try, is that the right way to use <code>yield from</code>, by returning the actual result from a function call?</li>
  <li>I think I need to add some sleep to my 2nd test case to wait for the task finish, but how should I do that? And how can I get my function calls to return in my 2nd test case?</li>
  <li>Is that a good way to implement asyncio with an existing module, by creating an async handler to handle requests?</li>
  <li>If the answer to question 2 is NO, does every client calls to the class <code>search</code> needs to include <code>loop = get_event_loop()</code> this kind of stuffs to async the requests?</li>
  </ol>