Reputation: 2649
According to realpython:
The end of the with block causes the ThreadPoolExecutor to do a .join() on each of the threads in the pool. It is strongly recommended that you use ThreadPoolExecutor as a context manager when you can so that you never forget to .join() the threads.
import logging
# import threading
import time
import concurrent.futures
def thread_function(name):
logging.info("Thread %s: starting", name)
time.sleep(2)
logging.info("Thread %s: finishing", name)
if __name__ == "__main__":
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO,
datefmt="%H:%M:%S")
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
executor.map(thread_function, range(3))
print("all done")
In the given code snippet, I was expecting that the last line - print("all done")
to be executed at the end (i.e. after all the threads are done executing). However, it is not? what am I missing?
Upvotes: 0
Views: 1226
Reputation: 4446
I think it has something to do with the way the Pycharm console buffers output (that you showed in your screenshot)
I run it several times in pycharm and it worked sometimes, while not working other times. However, when you run it in the terminal (or tell pycharm to "emulate terminal output") I don't see it anymore
Upvotes: 1