From 36941623dba0ba4c9671d784a17e0c5257f1cbf5 Mon Sep 17 00:00:00 2001 From: raynardj Date: Sat, 30 Apr 2022 18:27:02 +0800 Subject: [PATCH] async --- nbs/03_async.ipynb | 205 --------------------------------------------- 1 file changed, 205 deletions(-) delete mode 100644 nbs/03_async.ipynb diff --git a/nbs/03_async.ipynb b/nbs/03_async.ipynb deleted file mode 100644 index 0838ded..0000000 --- a/nbs/03_async.ipynb +++ /dev/null @@ -1,205 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Async\n", - "> Async toolset" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "heading_collapsed": true - }, - "source": [ - "## Async class" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "hidden": true - }, - "outputs": [], - "source": [ - "# default_exp asyncing" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "code_folding": [], - "hidden": true - }, - "outputs": [], - "source": [ - "# export\n", - "import asyncio\n", - "from typing import Callable\n", - "def Async(func: Callable):\n", - " \"\"\"\n", - " JS style async tool\n", - " def download_file(url):\n", - " ...\n", - " return path\n", - " \n", - " download_async = Async(download_file)\n", - " \n", - " download_async(url)\\\n", - " .then(lambda x: os.system(f\"tar -xvf {path}\"))\n", - " .catch(logging.error)\n", - " \n", - " \"\"\"\n", - " class AsyncFunc:\n", - " f\"\"\"{func.__doc__}\"\"\"\n", - " def __init__(self):\n", - " self.success=[]\n", - "\n", - " async def afunc(self, *args, **kwargs):\n", - " try:\n", - " result = func(*args, **kwargs)\n", - " for success_func in self.success:\n", - " result = success_func(result)\n", - " except Exception as e:\n", - " self.catch_func(e)\n", - " \n", - " def __repr__(self):\n", - " return f\"async function [{func}]\"\n", - "\n", - " def __call__(self, *args, **kwargs):\n", - " f\"{func.__doc__}\"\n", - " self.args = args\n", - " self.kwargs = kwargs\n", - " self.success = []\n", - " return self\n", - "\n", - " def then(self, success):\n", - " self.success.append(success)\n", - " return self\n", - "\n", - " def catch(self, catch):\n", - " assert len(self.success)>0,\\\n", - " \"You have to define self.then first\"\n", - " self.catch_func = catch\n", - " asyncio.gather(self.afunc(*self.args, **self.kwargs))\n", - "\n", - " return AsyncFunc()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## JS style async" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [], - "source": [ - "def slow_func(x):\n", - " \"\"\"took long calc\"\"\"\n", - " if x>0:\n", - " for i in range(x):\n", - " slow_func(x-1)\n", - " return x" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Creating async version of the function" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [], - "source": [ - "slow_func_async = Async(slow_func)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Testing async" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - ">> print from another task\n", - "CPU times: user 104 µs, sys: 5 µs, total: 109 µs\n", - "Wall time: 113 µs\n", - ">> input x is:10\n" - ] - } - ], - "source": [ - "%%time\n", - "slow_func_async(10)\\\n", - ".then(lambda x:f\">> input x is:{x}\")\\\n", - ".then(print)\\\n", - ".catch(print)\n", - "\n", - "print(\">> print from another task\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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.7.4" - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": false, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": false, - "toc_position": {}, - "toc_section_display": true, - "toc_window_display": false - } - }, - "nbformat": 4, - "nbformat_minor": 4 -}