Browse code

add example to time the runner in the notebook

Bas Nijholt authored on 30/10/2017 17:42:48
Showing 1 changed files
... ...
@@ -656,6 +656,61 @@
656 656
     "learner.plot().opts(style=dict(size=6)) * reconstructed_learner.plot()"
657 657
    ]
658 658
   },
659
+  {
660
+   "cell_type": "markdown",
661
+   "metadata": {},
662
+   "source": [
663
+    "### Timing functions"
664
+   ]
665
+  },
666
+  {
667
+   "cell_type": "markdown",
668
+   "metadata": {},
669
+   "source": [
670
+    "To time the runner you **cannot** simply use \n",
671
+    "```python\n",
672
+    "now = datetime.now()\n",
673
+    "runner = adaptive.Runner(...)\n",
674
+    "print(datetime.now() - now)\n",
675
+    "```\n",
676
+    "because this will be done immediately. Also blocking the kernel with `while not runner.task.done()` will not work because the runner will not do anything when the kernel is blocked.\n",
677
+    "\n",
678
+    "Therefore you need to create an `async` function and hook it into the `ioloop` like so:"
679
+   ]
680
+  },
681
+  {
682
+   "cell_type": "code",
683
+   "execution_count": null,
684
+   "metadata": {},
685
+   "outputs": [],
686
+   "source": [
687
+    "import asyncio\n",
688
+    "\n",
689
+    "async def time(runner):\n",
690
+    "    from datetime import datetime\n",
691
+    "    now = datetime.now()\n",
692
+    "    await runner.task\n",
693
+    "    return datetime.now() - now\n",
694
+    "\n",
695
+    "ioloop = asyncio.get_event_loop()\n",
696
+    "\n",
697
+    "learner = adaptive.learner.IntegratorLearner(f24, bounds=(0, 3), tol=1e-3)\n",
698
+    "runner = adaptive.Runner(learner, executor=adaptive.runner.SequentialExecutor(),\n",
699
+    "                         goal=lambda l: l.done())\n",
700
+    "\n",
701
+    "timer = ioloop.create_task(time(runner))"
702
+   ]
703
+  },
704
+  {
705
+   "cell_type": "code",
706
+   "execution_count": null,
707
+   "metadata": {},
708
+   "outputs": [],
709
+   "source": [
710
+    "# The result will only be set when the runner is done.\n",
711
+    "timer.result()"
712
+   ]
713
+  },
659 714
   {
660 715
    "cell_type": "markdown",
661 716
    "metadata": {},