{"id":2083,"date":"2022-08-30T15:21:54","date_gmt":"2022-08-30T15:21:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/25\/throwing-exceptions-node-js-vs-gevent-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:21:54","modified_gmt":"2022-08-30T15:21:54","slug":"throwing-exceptions-node-js-vs-gevent-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/throwing-exceptions-node-js-vs-gevent-collection-of-common-programming-errors\/","title":{"rendered":"Throwing exceptions: Node.js vs Gevent-Collection of common programming errors"},"content":{"rendered":"<p>Here is an exmaple code that shows that when a greenlet throws an uncaught excpetion, the whole process won&#8217;t be crashed. The code is here and the output is as following:<\/p>\n<pre><code>Traceback (most recent call last):  \nFile \"\/usr\/lib\/python2.7\/site-packages\/gevent\/greenlet.py\", line 327, in run  \n  result = self._run(*self.args, **self.kwargs)  \nFile \"test_exception_greenlet.py\", line 31, in _fail  \n  raise Exception('you fail')  \nException: you fail  \n failed with Exception  \n\nwin ready? True    \nfail ready? True  \nwin successful()? True  \nfail successful()? False  \nexception: Exception('you fail',)  \n<\/code><\/pre>\n<p>When using <code>gevent<\/code> to implemenet concurrency tasks, you should generate some <code>coroutines<\/code>, namely <code>greenlets<\/code>, and then the main thread should switch to a special <code>greenlet<\/code> named <strong>hub<\/strong> and then the loop happens among <code>greenlets<\/code>. The workflow is like this:<\/p>\n<blockquote>\n<p>main thread -&gt; one greenlet -&gt; hub greenlet -&gt; a greenlet -&gt; hub greenlet -&gt; a greenlet -&gt; &#8230; -&gt; some greenlet -&gt; main thread -&gt; &#8230; (maybe enters the loop again)<\/p>\n<\/blockquote>\n<p>From here, you should know how a greenlet runs internally. A greenlet is an instance of <code>Greenlet<\/code>. When a greenlet runs, it invokes its <code>run()<\/code> method. Here is the source code of <code>run()<\/code> in <code>Greenlet<\/code> class:<\/p>\n<pre><code>def run(self):\n    try:\n        if self._start_event is None:\n            self._start_event = _dummy_event\n        else:\n            self._start_event.stop()\n        try:\n            result = self._run(*self.args, **self.kwargs)\n        except:\n            self._report_error(sys.exc_info())\n            return\n        self._report_result(result)\n    finally:\n        self.__dict__.pop('_run', None)\n        self.__dict__.pop('args', None)\n        self.__dict__.pop('kwargs', None)\n<\/code><\/pre>\n<p>The <code>_run()<\/code> method here is a binding with a task you want to run as a greenlet. From here you could see that if an exception happens, it will be caught and reported by <code>_report_error()<\/code> method. By reading the source code of <code>_report_error()<\/code> and <code>_report_result()<\/code> which is invoked by the former, you know that it catches the exception and just the greenlet dies but not the whole process. When a greenlet raises an uncaught exception, it dies and the <code>hub<\/code> greenlet won&#8217;t schedule it any more.<\/p>\n<p>If you&#8217;re insterested in the implementation of <code>Greenlet<\/code>, here is the source code<\/p>\n<p id=\"rop\"><small>Originally posted 2013-12-25 10:55:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here is an exmaple code that shows that when a greenlet throws an uncaught excpetion, the whole process won&#8217;t be crashed. The code is here and the output is as following: Traceback (most recent call last): File &#8220;\/usr\/lib\/python2.7\/site-packages\/gevent\/greenlet.py&#8221;, line 327, in run result = self._run(*self.args, **self.kwargs) File &#8220;test_exception_greenlet.py&#8221;, line 31, in _fail raise Exception(&#8216;you fail&#8217;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2083","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2083"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}