{"id":722,"date":"2021-01-03T22:05:50","date_gmt":"2021-01-03T22:05:50","guid":{"rendered":"http:\/\/www.ishygddt.xyz\/~blog\/?p=722"},"modified":"2022-03-08T18:59:26","modified_gmt":"2022-03-08T18:59:26","slug":"python-with-epoll","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/01\/python-with-epoll","title":{"rendered":"Python: \"with epoll\""},"content":{"rendered":"<p>If you're using Python 3.4+, <code class=\"\" data-line=\"\">select.epoll<\/code> <a href=\"https:\/\/docs.python.org\/3\/library\/select.html#select.epoll\">already supports context<\/a> \u2014 you're clear to just write <code class=\"language-python\" data-line=\"\">with epoll() as E: \u2026<\/code> and everything will be OK. The epoll descriptor will get properly closed, detached, etc.<\/p>\n<p>But if you're operating on legacy 2 or \u22643.3 systems, writing bilingual code, etc., you might need to write a shim.<\/p>\n<p>Thankfully, this is straightforward:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">import select\nif hasattr(select.epoll, &#039;__enter__&#039;):\n\tepoll_with = select.epoll\nelse:\n\tclass epoll_with:\n\t\t&#039;&#039;&#039;contextlib wrapper for python2&#039;s epoll&#039;&#039;&#039;\n\t\tdef __init__(self, *args, **kwargs):\n\t\t\tself._E = select.epoll(*args, **kwargs)\n\t\tdef __enter__(self):\n\t\t\treturn self._E\n\t\tdef __exit__(self, exc_type, exc_value, traceback):\n\t\t\tself._E.close()\n\nwith epoll_with() as E:\n\tE.register(fd, flags)\n\t\u2026<\/code><\/pre>\n<p>Note that this isn't a true \"polyfill\" yet: the same class can't be used <em>both<\/em> directly <em>and<\/em> as a contextmanager (at least not in Python 2\/\u22643.3). I'll update this post with a proper polyfill at some point, but doing is non-trivial because \u201c<code class=\"language-python\" data-line=\"\">TypeError: type &#039;select.epoll&#039; is not an acceptable base type<\/code>\u201d.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you're using Python 3.4+, <code class=\"\" data-line=\"\">select.epoll<\/code> already supports context \u2014 you're clear to just write <code class=\"language-javascript\" data-line=\"\">with epoll() as E: \u2026<\/code> and everything will be OK. If not, here's how to shim it:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96,97],"tags":[52,51,44],"class_list":["post-722","post","type-post","status-publish","format-standard","hentry","category-howto","category-original-content","tag-epoll","tag-linux","tag-python"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=722"}],"version-history":[{"count":13,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/722\/revisions"}],"predecessor-version":[{"id":783,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/722\/revisions\/783"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=722"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}