Async API (Experimental)

Since Python 3.5, it is possible to write concurrent code using the async/await syntax.

This document describes Ray’s support for asyncio, which enables integration with popular async frameworks (e.g., aiohttp, aioredis, etc.) for high performance web and prediction serving.

Starting Ray

You must initialize Ray first.

Please refer to Starting Ray for instructions.

Converting Ray objects into asyncio futures

Ray object IDs can be converted into asyncio futures with ray.experimental.async_api.

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)[source]

Turn an object_id into a Future object.

Parameters:object_id – A Ray object_id.
Returns:A future object that waits the object_id.
Return type:PlasmaObjectFuture

Example Usage

Basic Python Distributed with Ray
# Execute f serially.


def f():
  time.sleep(1)
  return 1



results = [f() for i in range(4)]
# Execute f in parallel.


@ray.remote
def f():
    time.sleep(1)
    return 1

ray.init()
results = ray.get([f.remote() for i in range(4)])
Async Python Async Ray
# Execute f asynchronously.




async def f():
    await asyncio.sleep(1)
    return 1


loop = asyncio.get_event_loop()
tasks = [f() for i in range(4)]

results = loop.run_until_complete(
    asyncio.gather(tasks))
# Execute f asynchronously with Ray/asyncio.

from ray.experimental import async_api

@ray.remote
def f():
    time.sleep(1)
    return 1

ray.init()
loop = asyncio.get_event_loop()
tasks = [async_api.as_future(f.remote())
         for i in range(4)]
results = loop.run_until_complete(
    asyncio.gather(tasks))

Known Issues

Async API support is experimental, and we are working to improve its performance. Please let us know any issues you encounter.