title: How can I deal with "This event loop is already running" error on a nested function in asyncio?
body: <p>I want to execute web scraping with a set of categories, and each category also has a list of URLs. So I decided to call a function based only on each category in the main function, and within the inner function there is a non-blocking call.</p>
  
  <p>So here is the code:</p>
  
  <pre><code>def main():
      loop = asyncio.get_event_loop()
      b = loop.create_task(f("p", all_p_list))
      f = loop.create_task(f("f", all_f_list))
  
      loop.run_until_complete(asyncio.gather(p, f))
  </code></pre>
  
  <p>It should execute the <code>f</code> function concurrently. </p>
  
  <p>But the <code>f</code> function also has to run the loop, since in the function it calls a function simultaneously, based on each URL.</p>
  
  <pre><code>async def f(category, total): 
      urls = [urls_template[category].format(t) for t in t_list]
      soups_coro = map(parseURL_async, urls)
  
      loop = asyncio.get_event_loop()
      result = await loop.run_until_complete(asyncio.gather(*soups_coro))
  </code></pre>
  
  <p>But after I run the script, it got an <code>This event loop is already running</code> error, and I found that it is because I call <code>loop.run_until_complete()</code> in both inner and outer functions.</p>
  
  <p>However, when I strip the <code>run_until_complete()</code>, and just call <code>f()</code> in the <code>main()</code>, the function call immediately got finished and it cannot wait for the inner function to finish. So it is inevitable to call the loop in the <code>main()</code>. But then I think it is incompatible with the inner function, which also must call it.</p>
  
  <p>How can I deal with the problem and run the loop? The orinigal code is all in the same <code>main()</code> and it worked, but I want to make it cleaner if possible.</p>