ThreadPoolExecutor返回到任务数组

时间:2018-09-13 13:25:50

标签: python threadpoolexecutor

下面是我从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返回到简单变量task1,它将起作用。但是我的要求是将其返回到数组。所以当我这样做时     taskarr [0] = executor.submit(task(1,'A')) 它会引发错误,

TypeError: 'function' object does not support item assignment

0 个答案:

没有答案