Last active
June 21, 2022 06:11
-
-
Save soeque1/2083f7f803cf39d1f25c8da6a4dbcefb to your computer and use it in GitHub Desktop.
jupyter_background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from IPython.lib import backgroundjobs as bg\n", | |
"from threading import Timer\n", | |
"import time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Bg():\n", | |
" '''\n", | |
" RUN job at background\n", | |
" '''\n", | |
" def __init__(self):\n", | |
" super(Bg, self).__init__()\n", | |
" self.jobs=bg.BackgroundJobManager()\n", | |
" \n", | |
" def m(self, *args, **kwargs):\n", | |
" self.ts=time.time()\n", | |
" self.res=self.jobs.new(*args, **kwargs)\n", | |
" print ('Running')\n", | |
" Bg.isFin(self)\n", | |
" \n", | |
" def isFin(self):\n", | |
" if self.res.status == 'Completed':\n", | |
" print ('{}: {}'.format(__class__.__name__, self.res.status))\n", | |
" self.te=time.time()\n", | |
" return self.res.result\n", | |
" \n", | |
" timer=Timer(1, Bg.isFin, args=[self])\n", | |
" timer.start()\n", | |
" \n", | |
" def r(self):\n", | |
" if self.res.status == 'Running':\n", | |
" print ('Running')\n", | |
" else:\n", | |
" print ('%2.2f min' % ((self.te - self.ts)/60))\n", | |
" return self.res.result" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"test_m = Bg()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Starting job # 0 in a separate thread.\n", | |
"Running\n", | |
"Bg: Completed\n" | |
] | |
} | |
], | |
"source": [ | |
"test_m.m(time.sleep, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.05 min\n" | |
] | |
} | |
], | |
"source": [ | |
"test_m.r()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def test_multi(t=10):\n", | |
" time.sleep(t)\n", | |
" return t" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Starting job # 2 in a separate thread.\n", | |
"Running\n", | |
"Bg: Completed\n" | |
] | |
} | |
], | |
"source": [ | |
"test_m.m(test_multi, 3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.05 min\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"test_m.r()" | |
] | |
} | |
], | |
"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.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: