<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: pool code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 07 Oct 2008 15:36:30 GMT</pubDate>
    <description>DZone Snippets: pool code</description>
    <item>
      <title>Toggle Text on Button (Using thread timers)</title>
      <link>http://snippets.dzone.com/posts/show/4671</link>
      <description>// Works on a seperate thread and changes the color of a button if a significant event happens&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;void HotShotTick(object obj)&lt;br /&gt;{&lt;br /&gt;if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate {&lt;br /&gt;this.FillHotshots();&lt;br /&gt;if (this.dsSignificant.HotshotBets.Rows.Count &gt; 0)&lt;br /&gt;{&lt;br /&gt;this.btnHotShotBets.BackColor = Color.Red;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;this.btnHotShotBets.BackColor = Color.Green;&lt;br /&gt;}&lt;br /&gt;});&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Oct 2007 07:07:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4671</guid>
      <author>dubby (Dave)</author>
    </item>
    <item>
      <title>Thread Pool and Threaded Python decorators</title>
      <link>http://snippets.dzone.com/posts/show/4425</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;# vim:ts=4:sw=4:et&lt;br /&gt;# (c) 2007 under terms of LGPL v 2.1&lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt; &lt;br /&gt;&lt;br /&gt;from threading import Thread&lt;br /&gt;&lt;br /&gt;def threaded(func):&lt;br /&gt;    def proxy(*args, **kwargs):&lt;br /&gt;        thread = Thread(target=func, args=args, kwargs=kwargs)&lt;br /&gt;        thread.start()&lt;br /&gt;        return thread&lt;br /&gt;    return proxy&lt;br /&gt;&lt;br /&gt;from Queue import Queue&lt;br /&gt;&lt;br /&gt;class Pool(Queue):&lt;br /&gt;    def __init__(self, maxsize):&lt;br /&gt;        assert maxsize &gt; 0, 'maxsize &gt; 0 required for Pool class'&lt;br /&gt;        Queue.__init__(self, maxsize)&lt;br /&gt;        for i in range(maxsize):&lt;br /&gt;            thread = Thread(target = self._worker)&lt;br /&gt;            thread.setDaemon(True)&lt;br /&gt;            thread.start()&lt;br /&gt;&lt;br /&gt;    def _worker(self):&lt;br /&gt;        while True:&lt;br /&gt;            try:&lt;br /&gt;                func, args, kwargs = self.get()&lt;br /&gt;                func(*args, **kwargs)&lt;br /&gt;            except:&lt;br /&gt;                self.task_done()&lt;br /&gt;                self.join()&lt;br /&gt;                raise&lt;br /&gt;            self.task_done()&lt;br /&gt;&lt;br /&gt;    def addJob(self, func, *args, **kwargs):&lt;br /&gt;        self.put((func, args, kwargs))&lt;br /&gt;&lt;br /&gt;    def __enter__(self):&lt;br /&gt;        pass&lt;br /&gt;&lt;br /&gt;    def __exit__(self, exc_type, exc_value, traceback):&lt;br /&gt;        self.join()&lt;br /&gt;&lt;br /&gt;def threadpool(pool):&lt;br /&gt;    assert pool.__class__ == Pool, 'threadpool decorator require a Pool object'&lt;br /&gt;    def decorator(func):&lt;br /&gt;        def proxy(*args, **kwargs):&lt;br /&gt;            pool.put((func, args, kwargs))&lt;br /&gt;            return pool&lt;br /&gt;        return proxy&lt;br /&gt;    return decorator&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;example usage&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from threadpool import *&lt;br /&gt;&lt;br /&gt;from time import sleep&lt;br /&gt;from random import random&lt;br /&gt;&lt;br /&gt;pool = Pool(3)&lt;br /&gt;    &lt;br /&gt;@threadpool(pool)&lt;br /&gt;def test_threadpool(i):&lt;br /&gt;    print 'threadpool %i enter' % i&lt;br /&gt;    sleep(random())&lt;br /&gt;    print 'threadpool %i exit' % i&lt;br /&gt;&lt;br /&gt;print 'threadpool example'&lt;br /&gt;for i in range(6):&lt;br /&gt;    test_threadpool(i)&lt;br /&gt;pool.join()&lt;br /&gt;print 'done'&lt;br /&gt;print ''&lt;br /&gt;&lt;br /&gt;@threaded&lt;br /&gt;def test_threaded(i):&lt;br /&gt;    print 'threaded %i enter' % i&lt;br /&gt;    sleep(random())&lt;br /&gt;    print 'threaded %i exit' % i&lt;br /&gt;&lt;br /&gt;print 'threaded example'&lt;br /&gt;for i in range(5):&lt;br /&gt;    test_threaded(i)&lt;br /&gt;print 'done'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;result output&lt;br /&gt;&lt;br /&gt;threadpool example&lt;br /&gt;threadpool 0 enter&lt;br /&gt;threadpool 1 enter&lt;br /&gt;threadpool 2 enter&lt;br /&gt;threadpool 0 exit&lt;br /&gt;threadpool 3 enter&lt;br /&gt;threadpool 1 exit&lt;br /&gt;threadpool 4 enter&lt;br /&gt;threadpool 2 exit&lt;br /&gt;threadpool 5 enter&lt;br /&gt;threadpool 3 exit&lt;br /&gt;threadpool 5 exit&lt;br /&gt;threadpool 4 exit&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;threaded example&lt;br /&gt;done&lt;br /&gt;threaded 0 enter&lt;br /&gt;threaded 1 enter&lt;br /&gt;threaded 2 enter&lt;br /&gt;threaded 3 enter&lt;br /&gt;threaded 4 enter&lt;br /&gt;threaded 1 exit&lt;br /&gt;threaded 2 exit&lt;br /&gt;threaded 3 exit&lt;br /&gt;threaded 0 exit&lt;br /&gt;threaded 4 exit</description>
      <pubDate>Thu, 16 Aug 2007 12:55:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4425</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
  </channel>
</rss>
