Python asyncio.semaphore in async-await function
<p>I am trying to teach myself Python's async functionality. To do so I have built an async web scraper. I would like to limit the total number of connections I have open at once to be a good citizen on servers. I know that semaphore's are a good solution, and the asyncio library has a <a href="https://docs.python.org/3/library/asyncio-sync.html#semaphores" rel="noreferrer">semaphore</a> class built in. My issue is that Python complains when using <code>yield from</code> in an <code>async</code> function as you are combining <code>yield</code> and <code>await</code> syntax. Below is the exact syntax I am using...</p>

<pre><code>import asyncio
import aiohttp

sema = asyncio.BoundedSemaphore(5)

async def get_page_text(url):
    with (yield from sema):
        try:
            resp = await aiohttp.request('GET', url)
            if resp.status == 200:
                ret_val = await resp.text()
        except:
            raise ValueError
        finally:
            await resp.release()
    return ret_val
</code></pre>

<p>Raising this Exception:</p>

<pre><code>File "&lt;ipython-input-3-9b9bdb963407&gt;", line 14
    with (yield from sema):
         ^
SyntaxError: 'yield from' inside async function
</code></pre>

<p>Some possible solution I can think of...</p>

<ol>
<li>Just use the <code>@asyncio.coroutine</code> decorator</li>
<li>Use threading.Semaphore? This seems like it may cause other issues</li>
<li>Try this in the beta of Python 3.6 for <a href="https://docs.python.org/3.6/whatsnew/3.6.html#pep-525-asynchronous-generators" rel="noreferrer">this</a> reason.</li>
</ol>

<p>I am very new to Python's async functionality so I could be missing something obvious.</p>