return 的 ThreadPoolExecutor 到任务数组
return of ThreadPoolExecutor to array of tasks
下面是我从 https://tutorialedge.net/python/concurrency/python-threadpoolexecutor-tutorial/
获得的示例代码
from concurrent.futures import ThreadPoolExecutor
import threading
import random
taskarr = []
def task(n,c):
print("Executing our Task = {} and {}".format(n,c))
print("Task Executed {}".format(threading.current_thread()))
def main():
executor = ThreadPoolExecutor(max_workers=3)
task1 = executor.submit(task(1,'A'))
task2 = executor.submit(task(2,'B'))
if __name__ == '__main__':
main()
在此,如果 executor.submit 被 returned 到简单变量 task1,它就可以工作。但是我的要求是 return 它到一个数组。所以当我这样做的时候
taskarr[0] = executor.submit(任务(1,'A'))
它抛出错误,
TypeError: 'function' object does not support item assignment
我无法复制你的TypeError
;显然你还有一些你没有给我们看的代码。正如 rocksportrocker 在他们的 中指出的那样,您不能将值分配给尚不存在的索引,就像您尝试将 return 从 executor.submit()
分配给索引 0 一样空列表 typearr
。这将提高提到的 IndexError
.
但是,我可以生成一些 TypeError
,这是因为您错误地调用了 submit
。
Executor documentation 将参数 fn
定义为可调用对象,例如一个功能。现在,当您像这样调用 submit
时:
executor.submit(task(1,'A'))
task(1,'A')
在您的主线程中执行,然后将其 return 值(在您的情况下为 None
)传递给池线程。池线程然后尝试执行 None(1, 'A')
并引发一个 TypeError: 'NoneType' object is not callable
,您看不到它,因为它是在将来包装的。如果你调用 task1.result()
.
你会看到它
这意味着,执行任务的是您的主线程,而不是您的池线程,正如您在 task
:
中的第二条打印语句中应该看到的那样
Task Executed <_MainThread(MainThread, started 11064)>
为了让池线程完成工作,您需要将函数对象 task
传递给 executor.submit
,如下所示:
taskarr = []
def task(n,c):
print("Executing our Task = {} and {}".format(n,c))
time.sleep(3) # added this to make sure that two different threads from the pool get a task
print("Task Executed {}".format(threading.current_thread()))
def main():
executor = ThreadPoolExecutor(max_workers=3)
taskarr.append(executor.submit(task, 1,'A'))
taskarr.append(executor.submit(task, 2,'B'))
# to show that the main thread continues to execute
# while the pool threads work on the tasks
print("{}: tasks submitted".format(threading.current_thread().name))
if __name__ == '__main__':
main()
输出:
Executing our Task = 1 and A
Executing our Task = 2 and B
MainThread: tasks submitted
Task Executed <Thread(ThreadPoolExecutor-0_0, started daemon 12192)>
Task Executed <Thread(ThreadPoolExecutor-0_1, started daemon 11800)>
下面是我从 https://tutorialedge.net/python/concurrency/python-threadpoolexecutor-tutorial/
获得的示例代码from concurrent.futures import ThreadPoolExecutor
import threading
import random
taskarr = []
def task(n,c):
print("Executing our Task = {} and {}".format(n,c))
print("Task Executed {}".format(threading.current_thread()))
def main():
executor = ThreadPoolExecutor(max_workers=3)
task1 = executor.submit(task(1,'A'))
task2 = executor.submit(task(2,'B'))
if __name__ == '__main__':
main()
在此,如果 executor.submit 被 returned 到简单变量 task1,它就可以工作。但是我的要求是 return 它到一个数组。所以当我这样做的时候 taskarr[0] = executor.submit(任务(1,'A')) 它抛出错误,
TypeError: 'function' object does not support item assignment
我无法复制你的TypeError
;显然你还有一些你没有给我们看的代码。正如 rocksportrocker 在他们的 executor.submit()
分配给索引 0 一样空列表 typearr
。这将提高提到的 IndexError
.
但是,我可以生成一些 TypeError
,这是因为您错误地调用了 submit
。
Executor documentation 将参数 fn
定义为可调用对象,例如一个功能。现在,当您像这样调用 submit
时:
executor.submit(task(1,'A'))
task(1,'A')
在您的主线程中执行,然后将其 return 值(在您的情况下为 None
)传递给池线程。池线程然后尝试执行 None(1, 'A')
并引发一个 TypeError: 'NoneType' object is not callable
,您看不到它,因为它是在将来包装的。如果你调用 task1.result()
.
这意味着,执行任务的是您的主线程,而不是您的池线程,正如您在 task
:
Task Executed <_MainThread(MainThread, started 11064)>
为了让池线程完成工作,您需要将函数对象 task
传递给 executor.submit
,如下所示:
taskarr = []
def task(n,c):
print("Executing our Task = {} and {}".format(n,c))
time.sleep(3) # added this to make sure that two different threads from the pool get a task
print("Task Executed {}".format(threading.current_thread()))
def main():
executor = ThreadPoolExecutor(max_workers=3)
taskarr.append(executor.submit(task, 1,'A'))
taskarr.append(executor.submit(task, 2,'B'))
# to show that the main thread continues to execute
# while the pool threads work on the tasks
print("{}: tasks submitted".format(threading.current_thread().name))
if __name__ == '__main__':
main()
输出:
Executing our Task = 1 and A
Executing our Task = 2 and B
MainThread: tasks submitted
Task Executed <Thread(ThreadPoolExecutor-0_0, started daemon 12192)>
Task Executed <Thread(ThreadPoolExecutor-0_1, started daemon 11800)>