cozytaskpool

Cozy Task Pool for threaded concurrency based on tasks and channels.


Need help? Read Nimble

Cozy Task Pool

Just a repeating pattern of launching tasks concurrently in threads with threading/channels, extracted into its own micro package. Not very ergonomic, just hides some of the boilerplate of preparing and tearing down everything.

Requires --threads:on and (--mm:arc / --mm:atomicArc / --mm:orc)

Installation

Cozy Task Pool is in the nimble directory, use atlas or nimble to install:

atlas use cozytaskpool
nimble install cozytaskpool

Usage

Look at the executable part of the source, which is almost the copy of the following block:

import std/[tasks], threading/channels

var
  data = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61]
  pool: CozyTaskPool = newTaskPool()

proc log(inputData: int) =
  echo "Received some message about ", inputData

proc work(consumer: Chan[Task]; inputData: int) =
  doTheWork(inputData)
  consumer.send(toTask( log(inputData) ))

for x in data:
  pool.sendTask(toTask( work(pool.resultsChan(), x) ))

pool.stopPool()

License

Cozy Task Pool is licensed under GNU General Public License version 2.0 or later.

Author: indiscipline

Licence: GPL-2.0-or-later

Project website

Docs