<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: threading code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 05:32:11 GMT</pubDate>
    <description>DZone Snippets: threading code</description>
    <item>
      <title>Ruby Threading</title>
      <link>http://snippets.dzone.com/posts/show/5212</link>
      <description>Uses Ruby (version 1.8) threading to run code in parallel which reduces the time taken for the script to complete.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'ping' #from core classes&lt;br /&gt;@ip_prefix='192.168.1.'&lt;br /&gt;&lt;br /&gt;def ping_ip(i)&lt;br /&gt;  puts "Found #{@ip_prefix + i.to_s}" if Ping.pingecho(@ip_prefix+i.to_s,1)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;p50 = Thread.new{(1..50).each{|i| ping_ip(i) };puts 'p50-finished'}&lt;br /&gt;p100 = Thread.new{(51..100).each{|i| ping_ip(i) };puts 'p100-finished'}&lt;br /&gt;p150 = Thread.new{(101..150).each{|i| ping_ip(i) }; puts 'p150-finished'}&lt;br /&gt;p200 = Thread.new{(151..200).each{|i| ping_ip(i) }; puts 'p200-finished'}&lt;br /&gt;p254 = Thread.new{(201..254).each{|i| ping_ip(i) }; puts 'p254-finished'}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Found 192.168.1.2&lt;br /&gt;Found 192.168.1.3&lt;br /&gt;Found 192.168.1.8&lt;br /&gt;Found 192.168.1.9&lt;br /&gt;Found 192.168.1.10&lt;br /&gt;Found 192.168.1.11&lt;br /&gt;Found 192.168.1.12&lt;br /&gt;Found 192.168.1.13&lt;br /&gt;Found 192.168.1.15&lt;br /&gt;Found 192.168.1.110&lt;br /&gt;Found 192.168.1.21&lt;br /&gt;Found 192.168.1.22&lt;br /&gt;Found 192.168.1.140&lt;br /&gt;p50-finished&lt;br /&gt;p150-finished&lt;br /&gt;Found 192.168.1.200&lt;br /&gt;p200-finished&lt;br /&gt;p100-finished&lt;br /&gt;Found 192.168.1.254&lt;br /&gt;p254-finished&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This script took about 1 minute to complete, whereas the non-threaded script took around 4 minutes to complete.&lt;br /&gt;&lt;br /&gt;Reference: Ruby Threads: &lt;a href="http://rubylearning.com/satishtalim/ruby_threads.html"&gt;RUBY THREADS&lt;/a&gt; [rubylearning.com]&lt;br /&gt;&lt;br /&gt;*update 11:21pm*&lt;br /&gt;I tried the same experiment again this time with 26 threads (9 pings each on average) and the script finished in under 15 seconds.&lt;br /&gt;&lt;br /&gt;*update 11:39pm*&lt;br /&gt;A separate thread for each ping seems to work best, since it only took a couple of seconds to finish.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(1..254).each{|i| Thread.new {puts 'Found ' + ip_prefix + i.to_s if Ping.pingecho(ip_prefix+i.to_s,10)}}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 11 Mar 2008 23:00:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5212</guid>
      <author>jrobertson (James Robertson)</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>
