Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Ruby Threading

Uses Ruby (version 1.8) threading to run code in parallel which reduces the time taken for the script to complete.
require 'ping' #from core classes
@ip_prefix='192.168.1.'

def ping_ip(i)
  puts "Found #{@ip_prefix + i.to_s}" if Ping.pingecho(@ip_prefix+i.to_s,1)
end

p50 = Thread.new{(1..50).each{|i| ping_ip(i) };puts 'p50-finished'}
p100 = Thread.new{(51..100).each{|i| ping_ip(i) };puts 'p100-finished'}
p150 = Thread.new{(101..150).each{|i| ping_ip(i) }; puts 'p150-finished'}
p200 = Thread.new{(151..200).each{|i| ping_ip(i) }; puts 'p200-finished'}
p254 = Thread.new{(201..254).each{|i| ping_ip(i) }; puts 'p254-finished'}

output:
Found 192.168.1.2
Found 192.168.1.3
Found 192.168.1.8
Found 192.168.1.9
Found 192.168.1.10
Found 192.168.1.11
Found 192.168.1.12
Found 192.168.1.13
Found 192.168.1.15
Found 192.168.1.110
Found 192.168.1.21
Found 192.168.1.22
Found 192.168.1.140
p50-finished
p150-finished
Found 192.168.1.200
p200-finished
p100-finished
Found 192.168.1.254
p254-finished

This script took about 1 minute to complete, whereas the non-threaded script took around 4 minutes to complete.

Reference: Ruby Threads: RUBY THREADS [rubylearning.com]

*update 11:21pm*
I tried the same experiment again this time with 26 threads (9 pings each on average) and the script finished in under 15 seconds.

*update 11:39pm*
A separate thread for each ping seems to work best, since it only took a couple of seconds to finish.
(1..254).each{|i| Thread.new {puts 'Found ' + ip_prefix + i.to_s if Ping.pingecho(ip_prefix+i.to_s,10)}}

Thread Pool and Threaded Python decorators

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:et
# (c) 2007 under terms of LGPL v 2.1
# by Vsevolod S. Balashov <vsevolod@balashov.name> 

from threading import Thread

def threaded(func):
    def proxy(*args, **kwargs):
        thread = Thread(target=func, args=args, kwargs=kwargs)
        thread.start()
        return thread
    return proxy

from Queue import Queue

class Pool(Queue):
    def __init__(self, maxsize):
        assert maxsize > 0, 'maxsize > 0 required for Pool class'
        Queue.__init__(self, maxsize)
        for i in range(maxsize):
            thread = Thread(target = self._worker)
            thread.setDaemon(True)
            thread.start()

    def _worker(self):
        while True:
            try:
                func, args, kwargs = self.get()
                func(*args, **kwargs)
            except:
                self.task_done()
                self.join()
                raise
            self.task_done()

    def addJob(self, func, *args, **kwargs):
        self.put((func, args, kwargs))

    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_value, traceback):
        self.join()

def threadpool(pool):
    assert pool.__class__ == Pool, 'threadpool decorator require a Pool object'
    def decorator(func):
        def proxy(*args, **kwargs):
            pool.put((func, args, kwargs))
            return pool
        return proxy
    return decorator


example usage

from threadpool import *

from time import sleep
from random import random

pool = Pool(3)
    
@threadpool(pool)
def test_threadpool(i):
    print 'threadpool %i enter' % i
    sleep(random())
    print 'threadpool %i exit' % i

print 'threadpool example'
for i in range(6):
    test_threadpool(i)
pool.join()
print 'done'
print ''

@threaded
def test_threaded(i):
    print 'threaded %i enter' % i
    sleep(random())
    print 'threaded %i exit' % i

print 'threaded example'
for i in range(5):
    test_threaded(i)
print 'done'


result output

threadpool example
threadpool 0 enter
threadpool 1 enter
threadpool 2 enter
threadpool 0 exit
threadpool 3 enter
threadpool 1 exit
threadpool 4 enter
threadpool 2 exit
threadpool 5 enter
threadpool 3 exit
threadpool 5 exit
threadpool 4 exit
done

threaded example
done
threaded 0 enter
threaded 1 enter
threaded 2 enter
threaded 3 enter
threaded 4 enter
threaded 1 exit
threaded 2 exit
threaded 3 exit
threaded 0 exit
threaded 4 exit
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS