Popular recipes tagged "manager"http://code.activestate.com/recipes/tags/manager/2017-06-25T17:17:43-07:00ActiveState Code RecipesGuard against an exception in the wrong place (Python) 2017-06-25T17:17:43-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/580808-guard-against-an-exception-in-the-wrong-place/ <p style="color: grey"> Python recipe 580808 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/context/">context</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/guard/">guard</a>, <a href="/recipes/tags/manager/">manager</a>). Revision 2. </p> <p>Sometimes exception handling can obscure bugs unless you guard against a particular exception occurring in a certain place. One example is that <a href="https://www.python.org/dev/peps/pep-0479/">accidentally raising <code>StopIteration</code> inside a generator</a> will halt the generator instead of displaying a traceback. That was solved by PEP 479, which automatically has such <code>StopIteration</code> exceptions change to <code>RuntimeError</code>. See the discussion below for further examples.</p> <p>Here is a class which can be used as either a decorator or context manager for guarding against the given exceptions. It takes an exception (or a tuple of exceptions) as argument, and if the wrapped code raises that exception, it is re-raised as another exception type (by default <code>RuntimeError</code>).</p> <p>For example:</p> <pre class="prettyprint"><code>try: with exception_guard(ZeroDivisionError): 1/0 # raises ZeroDivisionError except RuntimeError: print ('ZeroDivisionError replaced by RuntimeError') @exception_guard(KeyError) def demo(): return {}['key'] # raises KeyError try: demo() except RuntimeError: print ('KeyError replaced by RuntimeError') </code></pre> Simple python thread manager (Python) 2012-12-17T16:19:12-08:00Tom Wissinghttp://code.activestate.com/recipes/users/4184629/http://code.activestate.com/recipes/578385-simple-python-thread-manager/ <p style="color: grey"> Python recipe 578385 by <a href="/recipes/users/4184629/">Tom Wissing</a> (<a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>A simple python static class handling basic threading. Designed for "worker" threads that loop forever over a function and the like.</p> Profile Manager (Cave Story) (Python) 2012-12-07T01:21:04-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578368-profile-manager-cave-story/ <p style="color: grey"> Python recipe 578368 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/profile/">profile</a>). </p> <p>If you have ever played a game that only had one save slot and wanted to be able to manage profiles, the following code written for Cave Story may be of use to you. The recipe provides a starting point for how one might go about writing a profile manager for such a program that runs through a command interface.</p> Simple python thread manager (Python) 2012-11-17T23:18:17-08:00Tino Deakhttp://code.activestate.com/recipes/users/4184300/http://code.activestate.com/recipes/578331-simple-python-thread-manager/ <p style="color: grey"> Python recipe 578331 by <a href="/recipes/users/4184300/">Tino Deak</a> (<a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>A simple python static class handling basic threading. Designed for "worker" threads that loop forever over a function and the like.</p> Distributed lock manager for Python (Python) 2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/ <p style="color: grey"> Python recipe 578194 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>). Revision 2. </p> <p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p> <p>First start server process:</p> <pre class="prettyprint"><code>$ chmod +x dlm.py $ ./dlm.py </code></pre> <p>Usage:</p> <pre class="prettyprint"><code>from dlm import LockClient client = LockClient('localhost', 27272, 'client_name') lock = client.mkLock('lock_name') lock.acquire() # critical section here... lock.release() # using context manager with lock: # critical section here... </code></pre>