【发布时间】:2018-02-09 11:37:11
【问题描述】:
我不明白为什么这段代码的行为方式不同。在第一种情况下,代码将打印“elo”,19 秒后我们将看到“3”。
在其他情况下,我们将首先等待 19 秒,然后我们将看到“elo”。
你能解释一下吗?
from concurrent.futures import ThreadPoolExecutor
def wait_on_future():
f = 3
import time
time.sleep(19)
print(f)
executor = ThreadPoolExecutor(max_workers=2)
executor.submit(wait_on_future)
print("elo")
对
from concurrent.futures import ThreadPoolExecutor
def wait_on_future():
f = 3
import time
time.sleep(19)
print(f)
with ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(wait_on_future)
print("elo")
【问题讨论】:
标签: python multithreading threadpoolexecutor