title: RuntimeError: This event loop is already running in python
body: <p>I think I'm getting this error because my code calls <code>asyncio.get_event_loop().run_until_complete(foo())</code> twice.  Once from <code>foo()</code> and second time from function called by <code>foo()</code>. My question is then: why should this be a problem? Why should I even care that this loop is running?</p>
  
  <hr>
  
  <p>There was an edit made to this question which, I think, obscured it (some people prefer to follow rules without understanding them, thus an "illegal" word was removed from the title).  Unfortunately, this creates confusion.</p>
  
  <p>I'm not surprised by the fact that the error is raised.  I can trace it back to the <code>asyncio</code> source and see that the authors of this library wanted to do it this way, there's no mystery there.  The puzzling part is in the reason the authors of the library decided it's illegal to ask from event loop to run some function to completion when the loop is already running.</p>
  
  <p>We can reduce the problem to just two such calls, and through case analysis we will see that these are the three possibilities:</p>
  
  <ol>
  <li>Neither of both functions ever terminates.</li>
  <li>One of the functions eventually terminates.</li>
  <li>Both functions eventually terminate.</li>
  </ol>
  
  <p>Now, is there any sane behavior which would address all three cases?  To me, it is obvious that there is, or, perhaps are multiple sane behaviors possible here.  For example:</p>
  
  <ol>
  <li>Nothing special, the execution of both functions is interleaved, and they keep running forever, just as expected.</li>
  <li>The loop doesn't return control to the code following the first instance of <code>run_until_complete()</code> until second function completes (thus no code after <code>run_until_complete()</code> will be executed.</li>
  <li>After the last function terminates, the loop returns control to the first code object which invoked <code>run_until_complete</code> ignoring all other invocation sites.</li>
  </ol>
  
  <p>Now, I can understand that this behavior may not be something that everyone would want.  But, since this library decided to give programmers control over starting / stopping the event loop, it should also meet the consequences of such decisions.  Making it an error to start the same loop multiple times precludes library code from ever doing this, which reduces the quality and usefulness of libraries utilizing <code>asyncio</code> (which is indeed the case with, for example, <code>aiohttp</code>).</p>