title: When using run_in_executor in asyncio, is the event loop executed in the main thread?
body: <p>I am experimenting with multi-thread execution using the asyncio feature.<br />
  It is my understanding that loop manages threads and executes functions on threads.<br />
  If this is the case, then it should not be possible to launch a new function while the main thread is still processing.<br />
  However, when I run the following code, it starts executing a new function while I am asleep. Could you tell me the reason?</p>
  <p>ref: <a href="https://stackoverflow.com/a/60747799">https://stackoverflow.com/a/60747799</a></p>
  <pre class="lang-py prettyprint-override"><code>import asyncio
  from concurrent.futures.thread import ThreadPoolExecutor
  from time import sleep
  import logging
  
  logging.basicConfig(
      level=logging.DEBUG, format=&quot;%(asctime)s %(thread)s %(funcName)s %(message)s&quot;
  )
  
  def long_task(t):
      &quot;&quot;&quot;Simulate long IO bound task.&quot;&quot;&quot;
      logging.info(&quot;2. t: %s&quot;, t)
      sleep(t)
      logging.info(&quot;5. t: %s&quot;, t)
      return t ** 2
  
  async def main():
      loop = asyncio.get_running_loop()
      executor = ThreadPoolExecutor(max_workers=2)
      inputs = range(1, 5)
      logging.info(&quot;1.&quot;)
      futures = [loop.run_in_executor(executor, long_task, i) for i in inputs]
      logging.info(&quot;3.&quot;)
      sleep(3)
      logging.info(&quot;4.&quot;)
      results = await asyncio.gather(*futures)
      logging.info(&quot;6.&quot;)
  
  if __name__ == &quot;__main__&quot;:
      asyncio.run(main())
  </code></pre>
  <p>expected output</p>
  <pre><code>2022-02-08 22:59:08,896 139673219430208 __init__ Using selector: EpollSelector
  2022-02-08 22:59:08,896 139673219430208 main 1.
  2022-02-08 22:59:08,897 139673194632960 long_task 2. t: 1
  2022-02-08 22:59:08,897 139673186240256 long_task 2. t: 2
  2022-02-08 22:59:08,897 139673219430208 main 3.
  2022-02-08 22:59:09,898 139673194632960 long_task 5. t: 1
  2022-02-08 22:59:10,898 139673186240256 long_task 5. t: 2
  2022-02-08 22:59:13,400 139673219430208 main 4.
  2022-02-08 22:59:09,898 139673194632960 long_task 2. t: 3
  2022-02-08 22:59:10,899 139673186240256 long_task 2. t: 4
  2022-02-08 22:59:12,902 139673194632960 long_task 5. t: 3
  2022-02-08 22:59:14,903 139673186240256 long_task 5. t: 4
  2022-02-08 22:59:14,903 139673219430208 main 6.
  </code></pre>
  <p>actual output</p>
  <pre><code>2022-02-08 22:59:08,896 139673219430208 __init__ Using selector: EpollSelector
  2022-02-08 22:59:08,896 139673219430208 main 1.
  2022-02-08 22:59:08,897 139673194632960 long_task 2. t: 1
  2022-02-08 22:59:08,897 139673186240256 long_task 2. t: 2
  2022-02-08 22:59:08,897 139673219430208 main 3.
  2022-02-08 22:59:09,898 139673194632960 long_task 5. t: 1
  2022-02-08 22:59:09,898 139673194632960 long_task 2. t: 3
  2022-02-08 22:59:10,898 139673186240256 long_task 5. t: 2
  2022-02-08 22:59:10,899 139673186240256 long_task 2. t: 4
  2022-02-08 22:59:12,902 139673194632960 long_task 5. t: 3
  2022-02-08 22:59:13,400 139673219430208 main 4.
  2022-02-08 22:59:14,903 139673186240256 long_task 5. t: 4
  2022-02-08 22:59:14,903 139673219430208 main 6.
  </code></pre>
  <p>Between 3 and 4, sleep(3) is being executed in the main thread. I understand that the end of longtask(1) and longtask(2) running earlier in Threadpool is printed during this time, but why is the next task running during this time? If event_loop is in the main thread, then sleep(3) should not allow the execution of the new function.</p>