<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>g-Off.net &#124; Geoffrey Foster &#187; threading</title>
	<atom:link href="http://g-Off.net/tag/threading/feed" rel="self" type="application/rss+xml" />
	<link>http://g-Off.net</link>
	<description></description>
	<lastBuildDate>Fri, 30 Oct 2009 00:12:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A Python repeatable threading.Timer class</title>
		<link>http://g-Off.net/software/a-python-repeatable-threadingtimer-class</link>
		<comments>http://g-Off.net/software/a-python-repeatable-threadingtimer-class#comments</comments>
		<pubDate>Wed, 18 Mar 2009 19:20:00 +0000</pubDate>
		<dc:creator>gfoster</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://g-Off.net/?p=11</guid>
		<description><![CDATA[I had a function that I wanted to be called repeatedly every x seconds but couldn't find a nice way of doing this. The python threading module has a Timer class which will wait for the given number of seconds and then execute the passed in callable object. Unfortunately, once it has executed that code [...]]]></description>
			<content:encoded><![CDATA[<p>I had a function that I wanted to be called repeatedly every x seconds but couldn't find a nice way of doing this. The python threading module has a Timer class which will wait for the given number of seconds and then execute the passed in callable object. Unfortunately, once it has executed that code it is no longer useable. So based upon that class I whipped up the following RepeatableTimer. It's not "hard real-time" or anything since it doesn't account for the time taken to execute the given function, but it's good enough for what I needed.<span id="more-11"></span> Feel free to use it however you see fit as I'm distributing it under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT</a> license.</p>
<h3>Usage:</h3>
<p> Create a new RepeatTimer object passing it required arguments and any optional ones.</p>
<h4>Required Arguments:</h4>
<ul title="required arguments">
<li>interval -- floating point number specifying the number of seconds to wait before executing function</li>
<li>function -- the function (or callable object) to be executed</li>
</ul>
<h4>Optional Arguments:</h4>
<ul title="optional arguments">
<li>iterations -- integer specifying the number of iterations to perform</li>
<li>args -- list of positional arguments passed to function</li>
<li>kwargs -- dictionary of keyword arguments passed to function</li>
</ul>
<h3>Examples:</h3>
<p>Execute the hello function every 5 seconds:</p>

<div class="wp_codebox"><table><tr id="p114"><td class="code" id="p11code4"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> hello<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello World!&quot;</span>
&nbsp;
r = RepeatTimer<span style="color: black;">&#40;</span><span style="color: #ff4500;">5.0</span>, hello<span style="color: black;">&#41;</span>
r.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Execute the hello function every 5 seconds but terminate when it has executed 10 times:</p>

<div class="wp_codebox"><table><tr id="p115"><td class="code" id="p11code5"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> hello<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello World!&quot;</span>
&nbsp;
r = RepeatTimer<span style="color: black;">&#40;</span><span style="color: #ff4500;">5.0</span>, hello, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
r.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table><tr id="p116"><td class="code" id="p11code6"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Copyright (c) 2009 Geoffrey Foster</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># Permission is hereby granted, free of charge, to any person</span>
<span style="color: #808080; font-style: italic;"># obtaining a copy of this software and associated documentation</span>
<span style="color: #808080; font-style: italic;"># files (the &quot;Software&quot;), to deal in the Software without</span>
<span style="color: #808080; font-style: italic;"># restriction, including without limitation the rights to use,</span>
<span style="color: #808080; font-style: italic;"># copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span style="color: #808080; font-style: italic;"># copies of the Software, and to permit persons to whom the</span>
<span style="color: #808080; font-style: italic;"># Software is furnished to do so, subject to the following</span>
<span style="color: #808080; font-style: italic;"># conditions:</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># The above copyright notice and this permission notice shall be</span>
<span style="color: #808080; font-style: italic;"># included in all copies or substantial portions of the Software.</span>
<span style="color: #808080; font-style: italic;"># </span>
<span style="color: #808080; font-style: italic;"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,</span>
<span style="color: #808080; font-style: italic;"># EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES</span>
<span style="color: #808080; font-style: italic;"># OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND</span>
<span style="color: #808080; font-style: italic;"># NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT</span>
<span style="color: #808080; font-style: italic;"># HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,</span>
<span style="color: #808080; font-style: italic;"># WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</span>
<span style="color: #808080; font-style: italic;"># FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR</span>
<span style="color: #808080; font-style: italic;"># OTHER DEALINGS IN THE SOFTWARE.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">threading</span> <span style="color: #ff7700;font-weight:bold;">import</span> Event, Thread
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RepeatTimer<span style="color: black;">&#40;</span>Thread<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, interval, function, iterations=<span style="color: #ff4500;">0</span>, args=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>, kwargs=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>:
        Thread.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">interval</span> = interval
        <span style="color: #008000;">self</span>.<span style="color: black;">function</span> = function
        <span style="color: #008000;">self</span>.<span style="color: black;">iterations</span> = iterations
        <span style="color: #008000;">self</span>.<span style="color: black;">args</span> = args
        <span style="color: #008000;">self</span>.<span style="color: black;">kwargs</span> = kwargs
        <span style="color: #008000;">self</span>.<span style="color: black;">finished</span> = Event<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        count = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">finished</span>.<span style="color: black;">is_set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">iterations</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> count <span style="color: #66cc66;">&lt;</span> <span style="color: #008000;">self</span>.<span style="color: black;">iterations</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">finished</span>.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">interval</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">finished</span>.<span style="color: black;">is_set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">function</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #008000;">self</span>.<span style="color: black;">args</span>, <span style="color: #66cc66;">**</span><span style="color: #008000;">self</span>.<span style="color: black;">kwargs</span><span style="color: black;">&#41;</span>
                count += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> cancel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">finished</span>.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>Updated</strong> <em>Oct 29, 2009</em>: Removed extra unneeded Event object.</p>
]]></content:encoded>
			<wfw:commentRss>http://g-Off.net/software/a-python-repeatable-threadingtimer-class/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
