Skip to content

Cozy Task Pool for threaded concurrency with Nim's channels

License

Notifications You must be signed in to change notification settings

indiscipline/cozytaskpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Cozy Task Pool for threaded concurrency with Nim's channels

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages