推荐学习书目
› 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 · 4403 views
    This topic created in 1871 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
    wd
    Sep 24, 2021 via iPhone
    1
    async 或者线程
    kealm
    kealm
    Sep 24, 2021
    2
    ```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
    ila
    Sep 24, 2021 via Android
    3
    celery
    Ct5T66PVR1bW7b2z
    Ct5T66PVR1bW7b2z
    Sep 24, 2021 via Android
    4
    threading.Thread
    rootit
    rootit
    Sep 24, 2021
    5
    python 肯定首选 asyncio.create_task() 了,再次 threading 起线程
    deplivesb
    deplivesb
    Sep 24, 2021
    6
    了解一下异步
    demonzoo
    demonzoo
    Sep 24, 2021
    7
    就直接在 a 里面调用 b() 就好了啊。。。不要 await b()
    IsaacYoung
    IsaacYoung
    Sep 24, 2021
    8
    thread
    meiyoumingzi6
    meiyoumingzi6
    Sep 24, 2021
    9
    哈哈哈哈哈哈 我当时刚接触 python 的时候也思考过这个问题

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