Created
November 9, 2016 23:31
-
-
Save tillahoffmann/b9c1a371f3e838c531534d0444bca708 to your computer and use it in GitHub Desktop.
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": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import threading\n", | |
"import sys\n", | |
"import traceback" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class MyThread(threading.Thread):\n", | |
" def run(self):\n", | |
" try:\n", | |
" # Do whatever you need to do\n", | |
" super(MyThread, self).run()\n", | |
" except Exception:\n", | |
" self.exc_info = sys.exc_info()\n", | |
" raise\n", | |
" \n", | |
" def print_exception(self):\n", | |
" exc_info = getattr(self, 'exc_info', None)\n", | |
" if exc_info:\n", | |
" traceback.print_exception(*exc_info)\n", | |
" else:\n", | |
" print(\"no exception occured\")\n", | |
" \n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Exception in thread Thread-4:\n", | |
"Traceback (most recent call last):\n", | |
" File \"/Users/tillhoffmann/miniconda3/envs/survey/lib/python3.5/threading.py\", line 914, in _bootstrap_inner\n", | |
" self.run()\n", | |
" File \"<ipython-input-2-1ed5dc0ce6fa>\", line 5, in run\n", | |
" super(MyThread, self).run()\n", | |
" File \"/Users/tillhoffmann/miniconda3/envs/survey/lib/python3.5/threading.py\", line 862, in run\n", | |
" self._target(*self._args, **self._kwargs)\n", | |
" File \"<ipython-input-3-dcb00ee6cf60>\", line 1, in <lambda>\n", | |
" t = MyThread(target=lambda: 1 / 0)\n", | |
"ZeroDivisionError: division by zero\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"t = MyThread(target=lambda: 1 / 0)\n", | |
"t.start()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Traceback (most recent call last):\n", | |
" File \"<ipython-input-2-1ed5dc0ce6fa>\", line 5, in run\n", | |
" super(MyThread, self).run()\n", | |
" File \"/Users/tillhoffmann/miniconda3/envs/survey/lib/python3.5/threading.py\", line 862, in run\n", | |
" self._target(*self._args, **self._kwargs)\n", | |
" File \"<ipython-input-3-dcb00ee6cf60>\", line 1, in <lambda>\n", | |
" t = MyThread(target=lambda: 1 / 0)\n", | |
"ZeroDivisionError: division by zero\n" | |
] | |
} | |
], | |
"source": [ | |
"t.print_exception()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"t = MyThread(target=lambda: 1 / 2)\n", | |
"t.start()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"no exception occured\n" | |
] | |
} | |
], | |
"source": [ | |
"t.print_exception()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment