ThreadPoolExecutor 执行没有参数的方法

2019 年 12 月 10 日
 lbfeng
def thread_function():
    logging.info("Thread %s: starting")
    time.sleep(2)
    logging.info("Thread %s: finishing")

with concurrent.futures.ThreadPoolExecutor(3) as executor:
   executor.map(thread_function)

如果 thread_function 没有参数的话,executor.map 是不是应该没有第二个参数.

4455 次点击
所在节点    Python
8 条回复
leon0318
2019 年 12 月 10 日
lbfeng
2019 年 12 月 10 日
@leonme 问之前就看了。。并没有提到 iterables 如果没有该怎么办
lbfeng
2019 年 12 月 10 日
关于 threadpoolexecutor map parameter 的都是 multiple parameter 的问题,没有 no parameter。。。
ClericPy
2019 年 12 月 10 日
该看的是 submit 得到 Future 后的用法吧

map 又不是万能的, 实在无聊那就

xx.map(lambda f: f(), [f])
guokeke
2019 年 12 月 10 日
是
wzwwzw
2019 年 12 月 10 日
用 submit /
lbfeng
2019 年 12 月 10 日
```
with concurrent.futures.ThreadPoolExecutor(3) as executor:
future = executor.submit(thread_function_no_parameter)
print(future.result())
```

如果想启动 3 个 thread,需要重复 executor.submit(thread_function_no_parameter) 3 遍??
wzwwzw
2019 年 12 月 11 日
@lbfeng 看下官方 demo future_to_url = {executor.submit(load_url, url, 60): url for url in URLS} 循环。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.ih06.com/t/627473

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX