ThreadPoolExecutor 执行没有参数的方法

2019-12-10 09:04:56 +08:00
 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 是不是应该没有第二个参数.

3452 次点击
所在节点    Python
8 条回复
leonme
2019-12-10 09:33:46 +08:00
lbfeng
2019-12-10 11:21:14 +08:00
@leonme 问之前就看了。。并没有提到 iterables 如果没有该怎么办
lbfeng
2019-12-10 11:23:41 +08:00
关于 threadpoolexecutor map parameter 的都是 multiple parameter 的问题,没有 no parameter。。。
ClericPy
2019-12-10 11:30:42 +08:00
该看的是 submit 得到 Future 后的用法吧

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

xx.map(lambda f: f(), [f])
guokeke
2019-12-10 11:35:07 +08:00
wzwwzw
2019-12-10 12:45:20 +08:00
用 submit /
lbfeng
2019-12-10 23:25:10 +08:00
```
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 09:10:25 +08:00
@lbfeng 看下官方 demo future_to_url = {executor.submit(load_url, url, 60): url for url in URLS} 循环。

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

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

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

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

© 2021 V2EX