Top-rated recipes tagged "handler" Code RecipesTrap KeyboardInterrupt and EOFError for graceful program termination (Python) 2016-11-13T20:17:03-08:00Vasudev Ram <p style="color: grey"> Python recipe 580718 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/error/">error</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/terminate/">terminate</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This recipe shows how to trap the KeyboardInterrupt and EOFError Python exceptions so that they do not crash your program. As a vehicle to show this, it uses a small Python utility that shows the ASCII code for any ASCII character you type.</p> RecursionError exception: concise and informative output (Python) 2015-07-05T23:46:59-07:00elazar <p style="color: grey"> Python recipe 578660 by <a href="/recipes/users/4187847/">elazar</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 3. </p> <p>Replaces the default exception hook with one that, upon "infinite recursion", removes the last cycle. This results in a significantly cleaner and shorter error message.</p> <p>Usage: simply import &lt;module&gt; as _</p> <p>For more details see the descussion here: <a href="" rel="nofollow"></a></p> Cosign Handler (Python) 2012-07-18T13:30:10-07:00Colin Higgs <p style="color: grey"> Python recipe 578217 by <a href="/recipes/users/4182866/">Colin Higgs</a> (<a href="/recipes/tags/authentication/">authentication</a>, <a href="/recipes/tags/cosign/">cosign</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/urllib/">urllib</a>). </p> <p>Handler (python 3.x urllib.request style) for web pages where cosign authentication is required.</p> <p>See <a href=""></a> for details of the cosign authentication system.</p> Multiprocess-safe logging file-handler + interprocess RLock (Python) 2010-09-22T17:30:10-07:00Jan Kaliszewski <p style="color: grey"> Python recipe 577395 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/rlock/">rlock</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threadsafe/">threadsafe</a>). Revision 11. </p> <p>A Python 2.x/3.x-compatibile <strong>multiprocess-safe logging file-handler</strong> (logging.FileHandler replacement, designed for logging to a single file from multiple independent processes) together with a simple <strong>interprocess recursive lock</strong> -- universal abstract classes + Unix/Linux implementation.</p> <p><strong>Update:</strong> It's is a deeply revised version. Especially, now it --</p> <ul> <li>is Python 2.4, 2.5, 2.6, 3.1 -compatibile (previously Py>=2.6 was needed); probably works also with 2.7, 3.0 and 3.2 (but not tested if it does);</li> <li>is multiprocess-safe as well as thread-safe (proviously thread safety within a process was missed);</li> <li>is based on public interfaces only (previously FileHandler._open() was called and overriden);</li> <li>implement full RLock instance interface, as documented for threading.RLock (previously non-blocking mode and context-manager interface were missing).</li> </ul> <p>The module contains:</p> <ul> <li>Unix/Linux-only example implementation (with flock-based locking): <strong>FLockRLock</strong> and <strong>FLockFileHandler</strong> classes.</li> <li>universal abstract classes -- which may be useful at developing implementation for non-Unix platforms: <strong>MultiprocessRLock</strong>, <strong>MultiprocessFileHandler</strong>, <strong>LockedFileHandler</strong>,</li> </ul> <p>Also a quick-and-dirty test was added.</p> <p><strong>It is still an alpha version -- I'll be very grateful for any feedback.</strong></p> <hr /> <p><strong>Further updates:</strong></p> <ul> <li><p>2010-09-20: Some corrections, especially: non-blocking mode bug in MultiprocessRLock.acquire() fixed; _test() function improved; plus fixes in the description below.</p></li> <li><p>2010-09-22: _test() improved and moved to description section. Mistaken copyright-notice removed.</p></li> </ul>