title: Unable to Yield From inside async function and get yielded data
body: <p>I am trying to Yield From a function from within an asynchronous function. Having spent hours trying to figure this out and traversing Stack Overflow to find similar questions previously answered but unable to help me find a solution to my own problem, I find myself here.</p>
  
  <p>Quite simply, I want to query the Asterisk Management Interface via Panoramisk, using a web browser and Websockets. When a user connects to the websocket server, it runs the ws_handle method</p>
  
  <pre><code>async def ws_handle(websocket, path):
      await register(websocket)
      try:
          async for message in websocket:
              data = json.loads(message)
              ...
  </code></pre>
  
  <p>I then want to retrieve some data, then deliver to the client. The problem which I am having, is that I find I am unable to just say</p>
  
  <pre><code>exts = yield from ExtensionStateList.get(AmiManager)
  </code></pre>
  
  <p>Where the ExtensionStateList.get function is (roughly) as below:</p>
  
  <pre><code>def get(AmiManager):
      queues_details = yield from AmiManager.send_action(
          {'Action': 'ExtensionStateList'})
  
      ...
  
      val = {
          'extensions': EXTENSIONS,
          'parks': PARKS,
          'paging': PAGING,
          'confrences': CONFRENCES,
          'apps': APPS,
          'misc': MISC
      }
  
      return val
  </code></pre>
  
  <p>I have used this same file, ExtensionStateList.py in another test file separate from my websockets server file, in a non-async method, calling it as shown before</p>
  
  <pre><code>exts = yield from ExtensionStateList.get(AmiManager)
  </code></pre>
  
  <p>without a problem, and it populates exts with the value returned from the function.</p>
  
  <p>My research leads me to iterate through it like so:</p>
  
  <pre><code>async for a in ExtensionStateList.get(AmiManager):
      yield a
  </code></pre>
  
  <p>but I do not know how I can use that to populate the variable I wish to populate. I have tried like this:</p>
  
  <pre><code>exts = ''
  async for a in ExtensionStatList.get(AmiManager):
      exts = exts+a
  </code></pre>
  
  <p>only to be told that it cannot join an AsyncIO.Future to a string. I have also tried swapping out the <code>return val</code> for a <code>yield val</code>, again with no luck.</p>
  
  <p>Evidently, to me, this is a shortcoming in my lacking knowledge of Python. What can I do? I was thinking that maybe I could change ExtensionStateList.get to async, but that would throw me back in the same boat I am in now?</p>
  
  <p><strong>ADDITIONALLY</strong></p>
  
  <p>I have continued scouring through StackOverflow, and found the following question:</p>
  
  <p><a href="https://stackoverflow.com/questions/39637675">What is the difference between @types.coroutine and @asyncio.coroutine decorators?</a></p>
  
  <p>It seems to me that maybe if I add <code>@asyncio.coroutine</code> on the line above <code>ws_handle</code>, like so:</p>
  
  <pre><code>@asyncio.coroutine
  async def ws_handle(websocket, path):
  </code></pre>
  
  <p>that I would then be able to:</p>
  
  <pre><code>exts = yield from ExtensionStateList.get(AmiManager)
  </code></pre>
  
  <p>However, I find that this does not work, and it tells me that I can not yield from inside an async function. Am I misunderstanding what I am reading here? Or am I maybe not implementing it correctly? Am I on the right track with this?</p>
  
  <p>As per the answer given here:</p>
  
  <p><a href="https://stackoverflow.com/questions/51583968/">&#39;yield from&#39; inside async function Python 3.6.5 aiohttp</a></p>
  
  <p>I have also tried awaiting the function like so:</p>
  
  <pre><code>exts = await ExtensionStateList.get(AmiManager)
  </code></pre>
  
  <p>However, Python tells me that the object generator cannot be used in await expression.</p>
  
  <p><strong>FURTHERMORE</strong></p>
  
  <p>For those who may be interested, this is how I am calling my ws_handle function. It is called upon creation of the websocket server, and the websocket server is responsible for <em>dispatching/calling?</em> the ws_handle function.</p>
  
  <p>It seems to me that it calls this function once for each client who connects and this function runs until the user disconnects.</p>
  
  <pre><code>WebsocketServer = websockets.serve(ws_handle, host, port)
  asyncio.get_event_loop().run_until_complete(WebsocketServer)
  asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <p><strong>ADDENDUM</strong></p>
  
  <p>Yes, again I add even more. I have modified my ExtensionStateList.py so that when calling the get method, it performs as per below:</p>
  
  <pre><code>async def get(AmiManager):
      val = await getInternal(AmiManager)
      return val
  
  @asyncio.coroutine
  def getInternal(AmiManager):
  </code></pre>
  
  <p>I can now use the <code>yield from</code> internally in the getInternal function, which was previously my get function, and I can call this and receive date as per below:</p>
  
  <pre><code>exts = await ExtensionStateList.get(AmiManager)
  </code></pre>
  
  <p>I think I am getting a grasp of this, and I see how they are two different ways of doing almost the same thing.</p>
  
  <p>Thanks for pointing me in the right direction guys!</p>