{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true, "pycharm": { "name": "#%% md\n" } }, "source": [ "## Asynchronous Flow\n", "\n", "Synchronous from outside, Jina runs asynchronously underneath: it manages the eventloop(s) for scheduling the jobs. In some scenario, user wants more control over the eventloop, then `AsyncFlow` comes to use. In the example below, Jina is part of the integration where another heavy-lifting job is running concurrently:" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import asyncio\n", "from jina import AsyncFlow\n", "\n", "async def run_async_flow_5s(): # WaitDriver pause 5s makes total roundtrip ~5s\n", " with AsyncFlow().add(uses='- !WaitDriver {}') as f:\n", " await f.index_ndarray(numpy.random.random([5, 4]), on_done=print)\n", "\n", "async def heavylifting(): # total roundtrip takes ~5s\n", " print('heavylifting other io-bound jobs, e.g. download, upload, file io')\n", " await asyncio.sleep(5)\n", " print('heavylifting done after 5s')\n", "\n", "async def concurrent_main(): # about 5s; but some dispatch cost, can't be just 5s, usually at <7s\n", " await asyncio.gather(run_async_flow_5s(), heavylifting())" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "`AsyncFlow` is very useful when using Jina inside Jupyter Notebook. As Jupyter/ipython already manages an eventloop and thanks to [`autoawait`](https://ipython.readthedocs.io/en/stable/interactive/autoawait.html), the following code can run out-of-the-box in Jupyter:" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "from jina import AsyncFlow\n", "import numpy\n", "\n", "with AsyncFlow().add() as f:\n", " await f.index_ndarray(numpy.random.random([5, 4]), on_done=print)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 0 }