concurrent.futures:同时进行运行多个任务
concurrent.futures: to run multiple task at the same time
下面,我尝试使用 Python 期货模块同时实现 运行 2 个功能。然而,
我遇到错误 future._condition.acquire() AttributeError: 'function' object has no attribute '_condition'
我是否正确使用了期货模块?
from concurrent.futures import ThreadPoolExecutor, as_completed
def f1():
print ("1")
return ('a')
def f2():
return 'b'
executors_list = []
with ThreadPoolExecutor(max_workers=2) as executor:
executors_list.append(f1)
executors_list.append(f2)
for future in as_completed(executors_list):
r = (future.result())
print("r: ".format(r))
需要使用executor.submit(f1)
from concurrent.futures import ThreadPoolExecutor, as_completed
def f1():
return ('a')
def f2():
return 'b'
executors_list = []
with ThreadPoolExecutor(max_workers=2) as executor:
to_do = [executor.submit(f1),
executor.submit(f2)]
for future in as_completed(to_do):
print("future: {}, result {} ".format(future, future.result()))
下面,我尝试使用 Python 期货模块同时实现 运行 2 个功能。然而,
我遇到错误 future._condition.acquire() AttributeError: 'function' object has no attribute '_condition'
我是否正确使用了期货模块?
from concurrent.futures import ThreadPoolExecutor, as_completed
def f1():
print ("1")
return ('a')
def f2():
return 'b'
executors_list = []
with ThreadPoolExecutor(max_workers=2) as executor:
executors_list.append(f1)
executors_list.append(f2)
for future in as_completed(executors_list):
r = (future.result())
print("r: ".format(r))
需要使用executor.submit(f1)
from concurrent.futures import ThreadPoolExecutor, as_completed
def f1():
return ('a')
def f2():
return 'b'
executors_list = []
with ThreadPoolExecutor(max_workers=2) as executor:
to_do = [executor.submit(f1),
executor.submit(f2)]
for future in as_completed(to_do):
print("future: {}, result {} ".format(future, future.result()))