Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing agent thread pools in Clojure

Tags:

clojure

Is there a way to control the thread pools which handle the functions which get sent to agents? As I understand things, if I send-off, underneath the hood I'm using an unbounded thread pool. I would like to, say, run some functions on one thread pool and other functions on another. The reason for this is say I have a some functions which do IO and which are also less important. I'd throw these on some bounded thread pool and wouldn't worry if there was excessive blocking and they stacked up since they're, well, less important. The main thing is that I wouldn't want their crappy IO blocking to say have an effect on some more important functions which are running on another thread pool.

I'm basing the question off of something similar I did with thread pools in Akka and I'm just wondering I can accomplish the same thing with Clojure.

like image 372
pondermatic Avatar asked Jul 03 '12 18:07

pondermatic


3 Answers

For Clojure versions up to 1.4:

You cannot replace the built-in agent send and send-off thread pools. They are hard-coded in Agent.java.

The send pool (used for computation) is fixed size = 2 + Runtime.getRuntime().availableProcessors().

The send-off pool (also used for futures) is a cached thread pool and will grow without bound. This allows an arbitrary number of background tasks to wait for I/O. The cached threads will be reused and discarded if they've been idle for one minute.

If you want to manage work on your own thread pool, you'll need to dip into java.util.concurrent or use a Clojure wrapper lib.

For Clojure 1.5 (upcoming):

You can supply your own ExecutorService using (send-via executor a f), the default threadpools are not hard-wired anymore. See Agent.java in Clojure 1.5+ for details.

like image 83
Alex Miller Avatar answered Nov 20 '22 11:11

Alex Miller


The Clojure library Claypoole is designed for exactly this. It lets you define threadpools and use (and reuse) them for futures, pmaps, and so on.

like image 29
Leon Barrett Avatar answered Nov 20 '22 10:11

Leon Barrett


Amit Rathore (of Runa inc), has published a library (called medusa) for managing thread pools. It looks like a fairly close match for what you are looking for.

http://s-expressions.com/2010/06/08/medusa-0-1-a-supervised-thread-pool-for-clojure-futures-2/

like image 39
Arthur Ulfeldt Avatar answered Nov 20 '22 12:11

Arthur Ulfeldt