推荐学习书目
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
minibear2021
V2EX  ›  Python

如何调用一个 function 而不用等待其返回

  •  
  •   minibear2021 ·
    minibear2021 · Sep 24, 2021 · 3901 views
    This topic created in 1693 days ago, the information mentioned may be changed or developed.
    比如现在有两个 function,一个 fun a,一个 fun b,在 fun a 中需要调用 fun b,但是对 b 的执行结果毫不关心,也不用等待 b 执行完,怎么实现比较合适?
    11 replies    2021-09-25 02:35:01 +08:00
    wd
        1
    wd  
       Sep 24, 2021 via iPhone
    async 或者线程
    kealm
        2
    kealm  
       Sep 24, 2021
    ```js
    async function b() {
    return new Promise((resolve) => setTimeout(resolve, 10000))
    }

    function a() {
    console.time('b')
    b()
    console.timeEnd('b')
    }

    async function aa() {
    console.time('b')
    await b()
    console.timeEnd('b')
    }
    ```
    ila
        3
    ila  
       Sep 24, 2021 via Android
    celery
    Ct5T66PVR1bW7b2z
        4
    Ct5T66PVR1bW7b2z  
       Sep 24, 2021 via Android
    threading.Thread
    rootit
        5
    rootit  
       Sep 24, 2021
    python 肯定首选 asyncio.create_task() 了,再次 threading 起线程
    deplivesb
        6
    deplivesb  
       Sep 24, 2021
    了解一下异步
    demonzoo
        7
    demonzoo  
       Sep 24, 2021
    就直接在 a 里面调用 b() 就好了啊。。。不要 await b()
    IsaacYoung
        8
    IsaacYoung  
       Sep 24, 2021
    thread
    meiyoumingzi6
        9
    meiyoumingzi6  
       Sep 24, 2021
    哈哈哈哈哈哈 我当时刚接触 python 的时候也思考过这个问题

    0. 多线程, 把 b 丢进新的线程计算
    1. 多进程, 同上
    3. 协程 async
    4. celcry 等异步框架
    5. 换 golang, 无脑 go 就行了 [手动狗头
    rammiah
        10
    rammiah  
       Sep 25, 2021 via Android
    golang:go 就完了
    dangyuluo
        11
    dangyuluo  
       Sep 25, 2021
    这种情况 Async 比多线程要好
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4614 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 00:10 · PVG 08:10 · LAX 17:10 · JFK 20:10
    ♥ Do have faith in what you're doing.