V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lbfeng
V2EX  ›  Python

ThreadPoolExecutor 执行没有参数的方法

  •  
  •   lbfeng · 2019-12-10 09:04:56 +08:00 · 3427 次点击
    这是一个创建于 1571 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 是不是应该没有第二个参数.

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

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

    xx.map(lambda f: f(), [f])
    guokeke
        5
    guokeke  
       2019-12-10 11:35:07 +08:00
    wzwwzw
        6
    wzwwzw  
       2019-12-10 12:45:20 +08:00
    用 submit /
    lbfeng
        7
    lbfeng  
    OP
       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
        8
    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} 循环。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4543 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:58 · PVG 17:58 · LAX 02:58 · JFK 05:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.