ray/doc/source/async_api.rst

79 lines
4.6 KiB
ReStructuredText
Raw Normal View History

Experimental asyncio support (#2015) * Init commit for async plasma client * Create an eventloop model for ray/plasma * Implement a poll-like selector base on `ray.wait`. Huge improvements. * Allow choosing workers & selectors * remove original design * initial implementation of epoll-like selector for plasma * Add a param for `worker` used in `PlasmaSelectorEventLoop` * Allow accepting a `Future` which returns object_id * Do not need `io.py` anymore * Create a basic testing model * fix: `ray.wait` returns tuple of lists * fix a few bugs * improving performance & bug fixing * add test * several improvements & fixing * fix relative import * [async] change code format, remove old files * [async] Create context wrapper for the eventloop * [async] fix: context should return a value * [async] Implement futures grouping * [async] Fix bugs & replace old functions * [async] Fix bugs found in tests * [async] Implement `PlasmaEpoll` * [async] Make test faster, add tests for epoll * [async] Fix code format * [async] Add comments for main code. * [async] Fix import path. * [async] Fix test. * [async] Compatibility. * [async] less verbose to not annoy the CI. * [async] Add test for new API * [async] Allow showing debug info in some of the test. * [async] Fix test. * [async] Proper shutdown. * [async] Lint~ * [async] Move files to experimental and create API * [async] Use async/await syntax * [async] Fix names & styles * [async] comments * [async] bug fixing & use pytest * [async] bug fixing & change tests * [async] use logger * [async] add tests * [async] lint * [async] type checking * [async] add more tests * [async] fix bugs on waiting a future while timeout. Add more docs. * [async] Formal docs. * [async] Add typing info since these codes are compatible with py3.5+. * [async] Documents. * [async] Lint. * [async] Fix deprecated call. * [async] Fix deprecated call. * [async] Implement a more reasonable way for dealing with pending inputs. * [async] Fix docs * [async] Lint * [async] Fix bug: Type for time * [async] Set our eventloop as the default eventloop so that we can get it through `asyncio.get_event_loop()`. * [async] Update test & docs. * [async] Lint. * [async] Temporarily print more debug info. * [async] Use `Poll` as a default option. * [async] Limit resources. * new async implementation for Ray * implement linked list * bug fix * update * support seamless async operations * update * update API * fix tests * lint * bug fix * refactor names * improve doc * properly shutdown async_api * doc * Change the table on the index page. * Adjust table size. * Only keeps `as_future`. * change how we init connection * init connection in `ray.worker.connect` * doc * fix * Move initialization code into the module. * Fix docs & code * Update pyarrow version. * lint * Restore index.rst * Add known issues. * Apply suggestions from code review Co-Authored-By: suquark <suquark@gmail.com> * rename * Update async_api.rst * Update async_api.py * Update async_api.rst * Update async_api.py * Update worker.py * Update async_api.rst * fix tests * lint * lint * replace the magic number
2018-12-06 17:39:05 -08:00
Async API (Experimental)
========================
Since Python 3.5, it is possible to write concurrent code using the ``async/await`` `syntax <https://docs.python.org/3/library/asyncio.html>`__.
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.
Converting Ray objects into asyncio futures
-------------------------------------------
Ray object IDs can be converted into asyncio futures with ``ray.experimental.async_api``.
.. code-block:: python
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']}
.. autofunction:: ray.experimental.async_api.as_future
Example Usage
-------------
+----------------------------------------+-----------------------------------------------------+
| **Basic Python** | **Distributed with Ray** |
+----------------------------------------+-----------------------------------------------------+
| .. code-block:: python | .. code-block:: python |
| | |
| # Execute f serially. | # Execute f in parallel. |
| | |
| | |
| def f(): | @ray.remote |
| time.sleep(1) | def f(): |
| return 1 | time.sleep(1) |
| | return 1 |
| | |
| | ray.init() |
| results = [f() for i in range(4)] | results = ray.get([f.remote() for i in range(4)]) |
+----------------------------------------+-----------------------------------------------------+
| **Async Python** | **Async Ray** |
+----------------------------------------+-----------------------------------------------------+
| .. code-block:: python | .. code-block:: python |
| | |
| # Execute f asynchronously. | # Execute f asynchronously with Ray/asyncio. |
| | |
| | from ray.experimental import async_api |
| | |
| | @ray.remote |
| async def f(): | def f(): |
| await asyncio.sleep(1) | time.sleep(1) |
| return 1 | return 1 |
| | |
| | ray.init() |
| loop = asyncio.get_event_loop() | loop = asyncio.get_event_loop() |
| tasks = [f() for i in range(4)] | tasks = [async_api.as_future(f.remote()) |
| | for i in range(4)] |
| results = loop.run_until_complete( | results = loop.run_until_complete( |
| asyncio.gather(tasks)) | asyncio.gather(tasks)) |
+----------------------------------------+-----------------------------------------------------+
Known Issues
------------
Async API support is experimental, and we are working to improve its performance. Please `let us know <https://github.com/ray-project/ray/issues>`__ any issues you encounter.