Most viewed recipes tagged "nonblocking"http://code.activestate.com/recipes/tags/nonblocking/views/2015-06-06T12:13:00-07:00ActiveState Code RecipesRun asynchronous tasks using coroutines (Python) 2010-08-06T16:16:20-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577129-run-asynchronous-tasks-using-coroutines/ <p style="color: grey"> Python recipe 577129 by <a href="/recipes/users/4173270/">Arnau Sanchez</a> (<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/gobject/">gobject</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pygtk/">pygtk</a>). Revision 20. </p> <p>This recipe shows a simple, transparent (and hopefully pythonic) way of running asynchronous tasks when writing a event-driven application (i.e. GUI). The aim is to allow a programmer to write time-consuming functions (usually IO-bound, but not only) with sequential-looking code, instead of scattering the logic over a bunch of callbacks. We will take advantage of the coroutines introduced in Python 2.5 (see <a href="http://www.python.org/dev/peps/pep-0342" rel="nofollow">http://www.python.org/dev/peps/pep-0342</a>). </p> <p>The goal: wouldn't it be great if we could write something like this?</p> <pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3): result1 = function_that_takes_eons_to_complete(arg1, arg2) result2 = another_function_that_downloads_a_big_really_big_file(result1, arg3) entry.set_text("The result is: %d" % result2) def on_start_button___clicked(button, entry): myjob(entry, 1, 2, 3) ... gtk.main() </code></pre> <p>Indeed, but we can't! The GUI will hang until the job is done and the user will be rightfully angry. Coroutines to the rescue: the absolute minimal change we can make to this code is transforming <em>myjob</em> into a coroutine and yield every time we do blocking stuff:</p> <pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3): result1 = yield some_task(arg1, arg2) result2 = yield some_other_task(result1, arg3) entry.set_text("The result is: %d" % result2) def on_start__clicked(button, entry): start_job(myjob(entry, 1, 2, 3)) </code></pre> <p><em>some_task</em> and <em>some_other_task</em> are here the asynchronous implementation of the sequential tasks used in the first fragment, and <em>start_job</em> the wrapper around the coroutine. Note that we still have to implement non-blocking versions of the tasks, but they are usually pretty generic (wait some time, download a file, ...) and can be re-used. If you happen to have a CPU-bound function or even a IO-bound code you cannot split (<em>urllib2</em> anyone?), you can always use a generic threaded task (granted, the whole point of using co-routines should be avoiding threads, but there is no alternative here).</p> <p>At the end, all the plumbing we need to make it work is just 1 function: <em>start_job</em> (wrapper around the job to manage the flow of the coroutine). The rest of the code -two asynchronous tasks (<em>sleep_task</em>, <em>threaded_task</em>) and a demo app- are shown solely as an example.</p> Non-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> Multicontext (e.g. asynchronous) inline execution framework using coroutines (Python) 2012-12-06T19:32:20-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576965-multicontext-eg-asynchronous-inline-execution-fram/ <p style="color: grey"> Python recipe 576965 by <a href="/recipes/users/4172294/">Glenn Eychaner</a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/inline/">inline</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 14. </p> <p>A framework for executing inline code, contained in a generator, across multiple execution contexts, by pairing it with an executor that handles the context switching at each yield. An example of a generator which executes some iterations synchronously and some asynchronously is provided. The framework is general enough to be applied to many different coroutine situations.</p> asyncore scheduler (Python) 2011-07-25T23:42:21-07:00Giampaolo RodolĂ http://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577808-asyncore-scheduler/ <p style="color: grey"> Python recipe 577808 by <a href="/recipes/users/4178764/">Giampaolo RodolĂ </a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/asyncore/">asyncore</a>, <a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/scheduler/">scheduler</a>, <a href="/recipes/tags/twisted/">twisted</a>). Revision 5. </p> <p>The thing I miss mostly in asyncore is a system for calling a function after a certain amount of time without blocking. This is crucial for simple tasks such as disconnecting a peer after a certain time of inactivity or more advanced use cases such as <a href="http://code.google.com/p/pyftpdlib/source/browse/tags/release-0.6.0/pyftpdlib/ftpserver.py#1048">bandwidth throttling</a>.</p> <p>This recipe was initially inspired by Twisted's internet.base.DelayedCall class:</p> <p><a href="http://twistedmatrix.com/trac/browser/tags/last_vfs_and_web2/twisted/internet/base.py#L34" rel="nofollow">http://twistedmatrix.com/trac/browser/tags/last_vfs_and_web2/twisted/internet/base.py#L34</a></p> <p>...then included into pyftpdlib:</p> <p><a href="http://code.google.com/p/pyftpdlib/issues/detail?id=72" rel="nofollow">http://code.google.com/p/pyftpdlib/issues/detail?id=72</a></p> <p>...and finally proposed for inclusion into asyncore:</p> <p><a href="http://bugs.python.org/issue1641" rel="nofollow">http://bugs.python.org/issue1641</a></p> Tail multiple pidgin IRC logfiles (Python) 2015-06-06T12:13:00-07:00Anton Vredegoorhttp://code.activestate.com/recipes/users/2667360/http://code.activestate.com/recipes/579066-tail-multiple-pidgin-irc-logfiles/ <p style="color: grey"> Python recipe 579066 by <a href="/recipes/users/2667360/">Anton Vredegoor</a> (<a href="/recipes/tags/colorize/">colorize</a>, <a href="/recipes/tags/irc/">irc</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/logfiles/">logfiles</a>, <a href="/recipes/tags/merging/">merging</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pidgin/">pidgin</a>, <a href="/recipes/tags/tail/">tail</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>Tail multiple pidgin IRC logfiles. </p> <p>Pidgin should be connected to IRC with the channels one wants to tail joined, and it should save logs as text.</p> <p>The script needs two arguments:</p> <pre class="prettyprint"><code>the directory containing the directories with channel logs a list of channel names, quoted and separated by spaces </code></pre> <p>Example command:</p> <p>python pidgin-irctail.py -d <a href="mailto:~/.purple/logs/irc/YOUR_IRC_HANDLE@irc.freenode.net">~/.purple/logs/irc/YOUR_IRC_HANDLE@irc.freenode.net</a> -c "#chan1 #chan2 #chan3"</p> <p>Some text elements are higlighted, and channel names are inserted into the log lines after the time info.</p> <p>If more than one channel is entered, the output of the logs is merged. </p>