我想知道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())
如果能有一个更精细的策略作为语言核心的母语,那就太好了。考虑过了吗?
从Python3.5开始,您可以为异步函数使用增强的生成器。
增强的生成器语法:
新的
async/await
语法:您可以使用Python 2.6中添加的multiprocessing module。您可以使用进程池,然后使用以下方法异步获取结果:
例如:
这只是一种选择。这个模块提供了很多工具来实现您想要的。而且,用这个做一个装饰师也很容易。
类似于:
有关详细信息,请参阅https://docs.python.org/library/threading.html上的文档。
相关问题 更多 >
编程相关推荐