I am experimenting with multi-thread execution using the asyncio feature.
It is my understanding that loop manages threads and executes functions on threads.
If this is the case, then it should not be possible to launch a new function while the main thread is still processing.
However, when I run the following code, it starts executing a new function while I am asleep. Could you tell me the reason?
ref: https://stackoverflow.com/a/60747799
import asyncio
from concurrent.futures.thread import ThreadPoolExecutor
from time import sleep
import logging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s"
)
def long_task(t):
"""Simulate long IO bound task."""
logging.info("2. t: %s", t)
sleep(t)
logging.info("5. t: %s", t)
return t ** 2
async def main():
loop = asyncio.get_running_loop()
executor = ThreadPoolExecutor(max_workers=2)
inputs = range(1, 5)
logging.info("1.")
futures = [loop.run_in_executor(executor, long_task, i) for i in inputs]
logging.info("3.")
sleep(3)
logging.info("4.")
results = await asyncio.gather(*futures)
logging.info("6.")
if __name__ == "__main__":
asyncio.run(main())
expected output
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.
actual output
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.
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.