Thread pool module for Pike

Async.pmod

Download it here.

Async provides a simple process pool that allocates a number of worker threads
that may then be utilized collectively without having to deal directly with
threads. Results of jobs sent to the pool are “future” objects, called Asyncs.
Asyncs’ values are then acquired by calling Async.sync, which blocks until the
value has been set by the pool. A supervisor thread monitors the worker threads
and restarts any that terminate abnormally.

Usage is simple:

// create a pool of four processes
Pool pool = Pool(4)
 
// send a job to the pool
Async result = Pool->send(exp, 10);
 
// get the result
result->sync();
// => 22026.46484375
 
// alternately, you can call an async
result();
// => 22026.46484375
 
// pools can conveniently map across a sequence
Async mapped_result = pool->map(exp, enumerate(5));
mapped_result->sync();
// => ({ 1.0, 2.718281745910645, 7.389056205749512, 20.08553695678711, 54.59814834594727 })
 
// or iterate without collecting return values
int x = 0;
pool->iter(lambda (int n) { x += n; }, enumerate(100));
write("%d", x);
// => 4950
 
// close the pool, blocking until all workers have terminated
pool->close();
Leave a comment | Trackback
Mar 10th, 2009 | Posted in Programming
No comments yet.