Popular Python recipes tagged "meta:requires=fcntl"http://code.activestate.com/recipes/langs/python/tags/meta:requires=fcntl/2014-06-30T20:30:57-07:00ActiveState Code RecipesNon-blocking readlines() (Python) 2014-06-30T20:30:57-07:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/578900-non-blocking-readlines/ <p style="color: grey"> Python recipe 578900 by <a href="/recipes/users/4190298/">Zack Weinberg</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>). </p> <p>A generator function which takes a file object (assumed to be some sort of pipe or socket, open for reading), and yields lines from it without blocking. If there is no input available, it will yield an endless stream of empty strings until input becomes available again; caller is responsible for not going into a busy loop. (Newlines are normalized but not stripped, so if there is actually a blank line in the input, the value yielded will be <code>'\n'</code>.) The intended use case is a thread which must respond promptly to input from a pipe, and also something else which cannot be fed to <code>select</code> (e.g. a <code>queue.Queue</code>). Note that the file object is ignored except for its <code>fileno</code>.</p> <p>Only tested on Unix. Only tested on 3.4; ought to work with any python that has <code>bytearray</code>, <code>locale.getpreferredencoding</code>, and <code>fcntl</code>.</p> Multiprocess-safe logging file-handler + interprocess RLock (Python) 2010-09-22T17:30:10-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577395-multiprocess-safe-logging-file-handler-interproces/ <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> fcntl.flock() (Unix file lock) behaviour sampling script (Python) 2010-09-22T00:11:12-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577404-fcntlflock-unix-file-lock-behaviour-sampling-scrip/ <p style="color: grey"> Python recipe 577404 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 3. </p> <p>A quick <em>fcntl.flock(fcntl.LOCK_EX | fcntl.LOCK_NB)</em> call sampling script: with <em>one file object</em> (and descriptor) or <em>separate file objects</em> (and different descriptors) pointing to the same filesystem path -- with/without <strong>threading</strong> or <strong>forking</strong>.</p> <p>It's rather exemplum-and-educational piece of code than utility-script, unless somebody has to few slots in their memory to remember that <strong>flock</strong> is file-descriptor-tacked (then quick run of the script can save web-searching) :)</p> POSIX context manager to temporarily silence, or filter lines from stdout (Python) 2010-03-03T06:17:23-08:00pwallerhttp://code.activestate.com/recipes/users/4173218/http://code.activestate.com/recipes/577083-posix-context-manager-to-temporarily-silence-or-fi/ <p style="color: grey"> Python recipe 577083 by <a href="/recipes/users/4173218/">pwaller</a> (<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/freopen/">freopen</a>, <a href="/recipes/tags/silence/">silence</a>, <a href="/recipes/tags/stdout/">stdout</a>, <a href="/recipes/tags/with/">with</a>). Revision 2. </p> <p>Fed up with libraries you don't have control over emitting text into your precious stdout?</p> <p>If they use stdout through python, then you can just change sys.stdout to be something else. If they are printing directly to stdout through a C module, or some other means, then you are stuck.</p> <p>.. at least until you discover the <code>with silence():</code> block!</p> <p>Caveats: Non-portable, tested only on 2.6 under Linux, uses threading.</p> <p>Example output:</p> <pre class="prettyprint"><code>$ python silence_file.py Before with block.. Sensible stuff! After the silence block </code></pre> Application lock (Python) 2009-08-22T13:00:35-07:00Max Polkhttp://code.activestate.com/recipes/users/4171523/http://code.activestate.com/recipes/576891-application-lock/ <p style="color: grey"> Python recipe 576891 by <a href="/recipes/users/4171523/">Max Polk</a> (<a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/locking/">locking</a>). </p> <p>Ensures application runs only once.</p> Subprocess with async I/O pipes class (Python) 2009-05-17T02:02:04-07:00Mike Kazantsevhttp://code.activestate.com/recipes/users/4170279/http://code.activestate.com/recipes/576759-subprocess-with-async-io-pipes-class/ <p style="color: grey"> Python recipe 576759 by <a href="/recipes/users/4170279/">Mike Kazantsev</a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/non_blocking_i_o/">non_blocking_i_o</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/subprocess/">subprocess</a>). Revision 2. </p> <p>Just stumbled upon the need to move data chunks between subprocesses in a non-linear way with some logic in-between, so tee(1) and fifo(7)'s weren't too good option. Inspired by 440554, but rewritten from scratch to remove unnecessary delays due to sleep(3) calls and suboptimal try/sleep-based polling.</p> Interrogating linux /dev/usb/hiddev0 in python (Python) 2009-07-07T01:32:17-07:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/576834-interrogating-linux-devusbhiddev0-in-python/ <p style="color: grey"> Python recipe 576834 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/hid/">hid</a>, <a href="/recipes/tags/hiddev/">hiddev</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/usb/">usb</a>). </p> <p>What this recipe does:</p> <p>Maps linux usb hid ioctls and related C structs to python; Call ioctls, make some sense of output. Prints all reports for the device with some info.</p> <p>Works with python 2.4 (tested python 2.4.6 on linux amd64). Would need changes (e.g. print) for python 3.0. Might need changes (ioctl signed/unsigned "FIX") for newer python than tested.</p> Simple shelve with Linux file locking (Python) 2008-12-21T05:50:07-08:00Michael Ihdehttp://code.activestate.com/recipes/users/4168518/http://code.activestate.com/recipes/576591-simple-shelve-with-linux-file-locking/ <p style="color: grey"> Python recipe 576591 by <a href="/recipes/users/4168518/">Michael Ihde</a> (<a href="/recipes/tags/dynamic_method/">dynamic_method</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/shelve/">shelve</a>). </p> <p>The shelve module is a easy way to add persistence to your application via a DBM database. However, if you have multiple reader/writer combination you need to lock the file to prevent corruption. The shelve module itself does not provide locking because it is platform specific. If you only need Linux, this simple module provide an easy way to support locking using dynamically added methods.</p> JSON RPC Server and client (Python) 2008-04-01T09:50:49-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/552751-json-rpc-server-and-client/ <p style="color: grey"> Python recipe 552751 by <a href="/recipes/users/4129454/">david decotigny</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This recipe shows how to create JSON RPC client and server objects. The aim is to mimic the standard python XML-RPC API both on the client and server sides, but using JSON marshalling. It depends on cjson (<a href="http://pypi.python.org/pypi/python-cjson" rel="nofollow">http://pypi.python.org/pypi/python-cjson</a>) for the encoding/decoding of JSON data. This recipe tries to reuse the code of XML-RPC as much as possible.</p> Low level inotify wrapper (Python) 2013-08-12T14:39:57-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/576375-low-level-inotify-wrapper/ <p style="color: grey"> Python recipe 576375 by <a href="/recipes/users/4035877/">Louis RIVIERE</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/inotify/">inotify</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/system/">system</a>). Revision 10. </p> <p>This module is meant to be as simple and straightforward as it gets.</p> Module to allow Asynchronous subprocess use on Windows and Posix platforms (Python) 2006-12-01T17:30:02-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/ <p style="color: grey"> Python recipe 440554 by <a href="/recipes/users/1241800/">Josiah Carlson</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 10. </p> <p>The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?</p> <p>Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).</p> <p>recv() and recv_err() both read at most maxsize bytes from the started subprocess. send() sends strings to the started subprocess. send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.</p> <p>If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.</p> <p>v. 1.3 fixed a few bugs relating to *nix support v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module. v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford. v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing. v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment. v. 1.9 Fixed fcntl calls for closed handles.</p> get names of all "up" network interfaces (linux only) (Python) 2005-08-11T13:17:36-07:00paul cannonhttp://code.activestate.com/recipes/users/2551140/http://code.activestate.com/recipes/439093-get-names-of-all-up-network-interfaces-linux-only/ <p style="color: grey"> Python recipe 439093 by <a href="/recipes/users/2551140/">paul cannon</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Uses the SIOCGIFCONF ioctl to obtain a list of interfaces and extracts those names, returning them in a list of strings.</p> portalocker - Cross-platform (posix/nt) API for flock-style file locking. (Python) 2008-05-16T21:12:08-07:00Jonathan Feinberghttp://code.activestate.com/recipes/users/1511/http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/ <p style="color: grey"> Python recipe 65203 by <a href="/recipes/users/1511/">Jonathan Feinberg</a> (<a href="/recipes/tags/files/">files</a>). Revision 7. </p> <p>Synopsis:</p> <p>import portalocker file = open("somefile", "r+") portalocker.lock(file, portalocker.LOCK_EX) file.seek(12) file.write("foo") file.close()</p> Query whether an interface is up on UNIX (Python) 2001-07-05T16:24:30-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/65449-query-whether-an-interface-is-up-on-unix/ <p style="color: grey"> Python recipe 65449 by <a href="/recipes/users/98061/">Jürgen Hermann</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This code shows how to call the low-level POSIX interface of Python, and handle the return values with the struct module.</p> Capturing the output and error streams from a unix shell command (Python) 2001-06-22T23:22:21-07:00Brent Burleyhttp://code.activestate.com/recipes/users/98036/http://code.activestate.com/recipes/52296-capturing-the-output-and-error-streams-from-a-unix/ <p style="color: grey"> Python recipe 52296 by <a href="/recipes/users/98036/">Brent Burley</a> (<a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>This recipe shows how to execute a unix shell command and capture the output and error streams in python. By contrast, os.system() sends both streams directly to the shell.</p>