作为this question的后续内容,我有一个小脚本,它启动threadpoolexecutor
来读取json
文件。在这样做的时候,我希望使用for
循环从1数到9。出于某些原因,即使我使用了executor.shutdown(wait=False)
,它仍然阻塞并等待read_employees
方法执行。在
If wait is False then this method will return immediately and the resources associated with the executor will be freed when all pending futures are done executing
import concurrent.futures
import json
import time
def read_employees(read_file):
with open(read_file) as f_obj:
employees = json.load(f_obj)
for emp in employees:
print(emp)
time.sleep(3)
def start_thread():
filename = 'employee.json'
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
executor.submit(read_employees, filename)
executor.shutdown(wait=False)
def print_number():
for num in range(1,10):
time.sleep(2)
print(num)
start_thread()
print_number()
如果我要这么做:
^{pr2}$它先从1数到9,然后打印出员工,延迟是因为在阅读员工时睡觉。是这样的:
1
2
3
4
5
6
7
8
9
ams@yourcompanyname.com
bcs@yourcompanyname.com
如何在不阻塞的情况下使用threadpoolexecutor
实现相同的输出?在
我建议您不要使用
with
语句。with
语句通过调用context manager的__exit__
方法来关闭。上下文管理器是实现__enter__
和__exit__
方法的任何类。因此,在with
语句中运行所有内容之后,它将调用传入的上下文管理器上的__exit__
。在在本例中,}的子类。因此,通过引用^{}'s class definition ,我们看到在它的
ThreadPoolExecutor
是一个上下文管理器。ThreadPoolExecutor
是{__exit__
方法中,它调用了self.shutdown(wait=True)
。在对
self.shutdown(wait=True)
的调用就是问题所在。如果遵循上下文管理器的工作方式,因为self.shutdown(wait=False)
是with
语句中的最后一个内容,__exit__
将直接在后面调用。这意味着self.shutdown(wait=True)
将被调用。所以这就是阻碍你的原因。在你有两个选择来解决这个问题。第一种方法是将
ThreadPoolExecutor
子类化并重写__exit__
方法。在第二种选择是这样做:
^{1}$可能是因为这个小片段:
如果使用
with
语句,则可以避免显式调用此方法,该语句将关闭Executor
(就像调用wait
时,wait
设置为True
一样等待)https://docs.python.org/3/library/concurrent.futures.html
相关问题 更多 >
编程相关推荐