Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Limit number of concurrent running threads in pool

Reply
Thread Tools

Limit number of concurrent running threads in pool

 
 
Joe Martin
Guest
Posts: n/a
 
      03-04-2010
Hi
I created a pool of threads (say, 500 threads) to process. However, due
to the weight of each thread, I want to limit the number of threads that
run concurrently.

So how would I go about putting a limit on the number of threads that
run at any given time? I would like to take, say, 5 threads from the
pool and run them, and as each one completes, it is removed from the
pool and is replaced with a new thread from the pool.

Could this be done with a "spy" thread, in that it constantly loops to
check how many threads are running at once, and if the number of running
threads falls below the limit of 5, it takes the next thread out of the
pool and runs it? Not sure how I would go about doing this, pretty new
to multithreading.

Thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Roger Pack
Guest
Posts: n/a
 
      03-04-2010
> I created a pool of threads (say, 500 threads) to process. However, due
> to the weight of each thread, I want to limit the number of threads that
> run concurrently.
>
> So how would I go about putting a limit on the number of threads that
> run at any given time? I would like to take, say, 5 threads from the
> pool and run them, and as each one completes, it is removed from the
> pool and is replaced with a new thread from the pool.


http://github.com/spox/actionpool

might help.
-r
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Joe Martin
Guest
Posts: n/a
 
      03-04-2010
Roger Pack wrote:
>> I created a pool of threads (say, 500 threads) to process. However, due
>> to the weight of each thread, I want to limit the number of threads that
>> run concurrently.
>>
>> So how would I go about putting a limit on the number of threads that
>> run at any given time? I would like to take, say, 5 threads from the
>> pool and run them, and as each one completes, it is removed from the
>> pool and is replaced with a new thread from the pool.

>
> http://github.com/spox/actionpool
>
> might help.
> -r


Thank you very much, Roger. I found this link
(http://snippets.dzone.com/posts/show/3276) which it looks like you had
a part in as well and just got that code working shortly after posting
this thread. But looking into ActionPool, it definitely offers expanded
functionality so I will probably implement that solution instead.

Cheers!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-05-2010
2010/3/4 Joe Martin <(E-Mail Removed)>:
> I created a pool of threads (say, 500 threads) to process. =A0However, du=

e
> to the weight of each thread, I want to limit the number of threads that
> run concurrently.
>
> So how would I go about putting a limit on the number of threads that
> run at any given time? =A0I would like to take, say, 5 threads from the
> pool and run them, and as each one completes, it is removed from the
> pool and is replaced with a new thread from the pool.
>
> Could this be done with a "spy" thread, in that it constantly loops to
> check how many threads are running at once, and if the number of running
> threads falls below the limit of 5, it takes the next thread out of the
> pool and runs it? =A0Not sure how I would go about doing this, pretty new
> to multithreading.


Why do you create a pool much larger than the load you want to accept?
Usually the pool size is used to limit concurrency. Actually that is
the main purpose of thread pools.

If you have different tasks for which you want to have different
limits on concurrency you could also create several pools with
different sizes.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Chuck Remes
Guest
Posts: n/a
 
      03-05-2010

On Mar 4, 2010, at 12:49 PM, Joe Martin wrote:

> Hi
> I created a pool of threads (say, 500 threads) to process. However, due
> to the weight of each thread, I want to limit the number of threads that
> run concurrently.
>
> So how would I go about putting a limit on the number of threads that
> run at any given time? I would like to take, say, 5 threads from the
> pool and run them, and as each one completes, it is removed from the
> pool and is replaced with a new thread from the pool.
>
> Could this be done with a "spy" thread, in that it constantly loops to
> check how many threads are running at once, and if the number of running
> threads falls below the limit of 5, it takes the next thread out of the
> pool and runs it? Not sure how I would go about doing this, pretty new
> to multithreading.


I've had very good success using the Threadz gem.

http://github.com/nanodeath/threadz

It's quite easy to understand and works very well with MRI and JRuby.

cr


 
Reply With Quote
 
Joe Martin
Guest
Posts: n/a
 
      03-05-2010
Robert Klemme wrote:
> 2010/3/4 Joe Martin <(E-Mail Removed)>:
>> check how many threads are running at once, and if the number of running
>> threads falls below the limit of 5, it takes the next thread out of the
>> pool and runs it? ´┐ŻNot sure how I would go about doing this, pretty new
>> to multithreading.

>
> Why do you create a pool much larger than the load you want to accept?
> Usually the pool size is used to limit concurrency. Actually that is
> the main purpose of thread pools.
>
> If you have different tasks for which you want to have different
> limits on concurrency you could also create several pools with
> different sizes.
>
> Kind regards
>
> robert


When running this program, I will provide a list of items that need
processing. In some cases, this list can be as long as 250 items, in
other cases well over 50,000. The processing of each item can take
anywhere from 15 to 60 seconds per item, so you can see there is a
benefit to multithreading here. In processing each item, there are also
a number of database calls that occur, so I would like to put a cap on
the number of actively running threads to avoid overwhelming the
database. Am I going about this the wrong way? Is there a more
effecient more suitable way of doing this?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-05-2010
2010/3/5 Joe Martin <(E-Mail Removed)>:
> Robert Klemme wrote:
>> 2010/3/4 Joe Martin <(E-Mail Removed)>:
>>> check how many threads are running at once, and if the number of runnin=

g
>>> threads falls below the limit of 5, it takes the next thread out of the
>>> pool and runs it? =EF=BF=BDNot sure how I would go about doing this, pr=

etty new
>>> to multithreading.

>>
>> Why do you create a pool much larger than the load you want to accept?
>> =C2=A0Usually the pool size is used to limit concurrency. =C2=A0Actually=

that is
>> the main purpose of thread pools.
>>
>> If you have different tasks for which you want to have different
>> limits on concurrency you could also create several pools with
>> different sizes.
>>
>> Kind regards
>>
>> robert

>
> When running this program, I will provide a list of items that need
> processing. =C2=A0In some cases, this list can be as long as 250 items, i=

n
> other cases well over 50,000. =C2=A0The processing of each item can take
> anywhere from 15 to 60 seconds per item, so you can see there is a
> benefit to multithreading here. =C2=A0In processing each item, there are =

also
> a number of database calls that occur, so I would like to put a cap on
> the number of actively running threads to avoid overwhelming the
> database. =C2=A0Am I going about this the wrong way? =C2=A0Is there a mor=

e
> effecient more suitable way of doing this?


For this scenario a thread pool with fixed size seems sufficient.

queue =3D Queue.new # or bounded queue

def cont(item) !item.nil? end

threads =3D (1..10).map do
Thread.new do
while (cont(item =3D queue.deq))
# .. process
end
end
end

queue.enc "Task"

threads.size.times do
queue.enq nil # terminate
end

threads.each {|th| th.join}

As simple as that.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Chuck Remes
Guest
Posts: n/a
 
      03-05-2010
On Mar 5, 2010, at 7:06 AM, Joe Martin wrote:

> Robert Klemme wrote:
>> 2010/3/4 Joe Martin <(E-Mail Removed)>:
>>> check how many threads are running at once, and if the number of =

running
>>> threads falls below the limit of 5, it takes the next thread out of =

the
>>> pool and runs it? =EF=BF=BDNot sure how I would go about doing this, =

pretty new
>>> to multithreading.

>>=20
>> Why do you create a pool much larger than the load you want to =

accept?
>> Usually the pool size is used to limit concurrency. Actually that is
>> the main purpose of thread pools.
>>=20
>> If you have different tasks for which you want to have different
>> limits on concurrency you could also create several pools with
>> different sizes.
>>=20
>> Kind regards
>>=20
>> robert

>=20
> When running this program, I will provide a list of items that need=20
> processing. In some cases, this list can be as long as 250 items, in=20=


> other cases well over 50,000. The processing of each item can take=20
> anywhere from 15 to 60 seconds per item, so you can see there is a=20
> benefit to multithreading here. In processing each item, there are =

also=20
> a number of database calls that occur, so I would like to put a cap on=20=


> the number of actively running threads to avoid overwhelming the=20
> database. Am I going about this the wrong way? Is there a more=20
> effecient more suitable way of doing this?


The Threadz gem let's you create a thread pool and then wait on its =
completion before you add more to it. This mechanism will help you cap =
the number of threads making database calls.

cr


 
Reply With Quote
 
Joe Martin
Guest
Posts: n/a
 
      03-05-2010
Robert Klemme wrote:
> For this scenario a thread pool with fixed size seems sufficient.
>


Very good. This works quite nicely as well.

Just wondering, are there any performance benefits of using one method
over the other?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Caleb Clausen
Guest
Posts: n/a
 
      03-05-2010
On 3/5/10, Joe Martin <(E-Mail Removed)> wrote:
> Robert Klemme wrote:
>> For this scenario a thread pool with fixed size seems sufficient.
>>

>
> Very good. This works quite nicely as well.
>
> Just wondering, are there any performance benefits of using one method
> over the other?


Yes. Threads use memory (quite a lot of it, in fact). Mostly this goes
to the thread's stack. Limiting the number of threads saves quite a
bit of memory. I'm not sure there's any improvement in the amount of
cpu time either way, other than perhaps some fewer cache misses
resulting from using less memory.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic NAT pool doesn't report full when pool contains interface IP tom Cisco 0 10-09-2009 02:22 AM
does python have a generic object pool like commons-pool in Java Rick Lawson Python 8 07-16-2009 11:25 PM
PIX 501 issue routing between VPN pool and local pool eostrike Cisco 3 10-24-2008 09:43 PM
problems locating the concurrent EDU.oswego.cs.dl.util.concurrent package Pep Java 6 08-16-2005 07:26 AM
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. Guoqi Zheng ASP .Net 4 06-03-2004 06:39 PM



Advertisments