pondermatic
pondermatic

Reputation: 6583

Managing agent thread pools in 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.

Upvotes: 7

Views: 5065

Answers (3)

Leon Barrett
Leon Barrett

Reputation: 121

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.

Upvotes: 2

Alex Miller
Alex Miller

Reputation: 70211

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.

Upvotes: 14

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

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/

Upvotes: 1

Related Questions