Aiohttp, Asyncio: RuntimeError: Event loop is closed
<p>I have two scripts, scraper.py and db_control.py. In scraper.py I have something like this:</p>
<pre><code>...
def scrape(category, field, pages, search, use_proxy, proxy_file):
    ...
    loop = asyncio.get_event_loop()

    to_do = [ get_pages(url, params, conngen) for url in urls ]
    wait_coro = asyncio.wait(to_do)
    res, _ = loop.run_until_complete(wait_coro)
    ...
    loop.close()
    
    return [ x.result() for x in res ]

...
</code></pre>
<p>And in db_control.py:</p>
<pre><code>from scraper import scrape
...
while new &lt; 15:
    data = scrape(category, field, pages, search, use_proxy, proxy_file)
    ...
...
</code></pre>
<p>Theoretically, scraper should be started unknown-times until enough of data have been obtained. But when <code>new</code> is not imidiatelly <code>&gt; 15</code> then this error occurs:</p>
<pre><code>  File &quot;/usr/lib/python3.4/asyncio/base_events.py&quot;, line 293, in run_until_complete
self._check_closed()
  File &quot;/usr/lib/python3.4/asyncio/base_events.py&quot;, line 265, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
</code></pre>
<p>But scripts works just fine if I run scrape() only once. So I guess there is some problem with recreating <code>loop = asyncio.get_event_loop()</code>, I have tried <a href="https://stackoverflow.com/questions/32598231/asyncio-runtimeerror-event-loop-is-closed">this</a> but nothing changed. How I can fix this? Of course those are just snippets of my code, if you think problem can be elsewhere, full code is available <a href="https://github.com/kongzii/korpi/tree/master/korpi/lib" rel="nofollow noreferrer">here</a>.</p>