python - Python中的异步方法调用?

我想知道 Python 中是否有任何用于异步方法调用的库.如果你能做类似的事情会很棒

@async
def longComputation():
    <code>


token = longComputation()
token.registerCallback(callback_function)
# alternative, polling
while not token.finished():
    doSomethingElse()
    if token.finished():
        result = token.result()

或者异步调用非异步例程

def longComputation()
    <code>

token = asynccall(longComputation())

如果在语言核心中有一个更精细的策略作为本地语言,那就太好了。考虑过吗?

最佳答案

类似:

import threading

thr = threading.Thread(target=foo, args=(), kwargs={})
thr.start() # Will run "foo"
....
thr.is_alive() # Will return whether foo is running currently
....
thr.join() # Will wait till "foo" is done

参见 https://docs.python.org/library/threading.html 上的文档了解更多详情。

https://stackoverflow.com/questions/1239035/

相关文章:

ruby - 即使在使用 rvm pkg install zlib 后也无法加载此类文件 - zli

windows - Microsoft Windows 的终端多路复用器 - GNU Screen

python - 如何使用 Python 将整个文件目录复制到现有目录中?

python - 如何将字符串复制到剪贴板?

linux - 如何在 Linux shell 中对变量进行除法?

python - "for line in..."导致 UnicodeDecodeError : '

linux - 如何查看符号链接(symbolic link)的完整绝对路径

linux - 在 tmux 客户端之间移动窗口

python - 名称错误 : name 'reduce' is not defined in Py

linux - 如何列出(ls)目录中最后修改的 5 个文件?