{ "cells": [ { "cell_type": "markdown", "metadata": { "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, "metadata": {}, "outputs": [], "source": [ "#Run this command to install Jina in this notebook \n", "!pip install jina" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import asyncio\n", "from jina import AsyncFlow\n", "from jina.types.document.generators import from_ndarray\n", "\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(from_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())" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "from jina.types.document.generators import from_ndarray\n", "from jina import AsyncFlow\n", "import numpy\n", "\n", "with AsyncFlow().add() as f:\n", " f.index(from_ndarray(numpy.random.random([5, 4])), on_done=print)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }