从Python 3.5开始,就可以使用async/ wait语法编写并发代码。
本文档描述了Ray对asyncio的支持,它支持与流行的异步框架(如aiohttp、aioredis等)集成,以提供高web性能和预测服务。主要通过具体的例子说明Ray对象的转换为asyncio期货,和异步的四种情况。
将Ray对象转换为asyncio期货
可以使用ray.experimental.async_api
将Ray对象id转换为asyncio期货
import asyncio
import time
import ray
from ray.experimental import async_api
@ray.remote
def f():
time.sleep(1)
return {'key1': ['value']}
ray.init()
future = async_api.as_future(f.remote())
asyncio.get_event_loop().run_until_complete(future) # {'key1': ['value']}
ray.experimental.async_api.as_future(object_id)
将object_id
转换为期货(future)对象
Parameters:
object_id – 一个Ray的对象ID( object_id)。
Returns:
object_id转换后的期货对象。
Return type:
PlasmaObjectFuture
示例
基 |
---|