This commit is contained in:
Jaremy Creechley 2024-02-09 22:20:46 -07:00
parent 65a6f4fc77
commit 51459a8c9b
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 57 additions and 0 deletions

17
src/apatheia/jobs.nim Normal file
View File

@ -0,0 +1,17 @@
import ./queues
import taskpools
export queues
type
JobQueue*[T] = object
queue*: SignalQueue[T]
taskpool*: Taskpool
proc newJobQueue*[T](maxItems: int = 0, taskpool: Taskpool = Taskpool.new()): JobQueue[T] {.raises: [ApatheiaSignalErr].} =
JobQueue[T](queue: newSignalQueue[T](maxItems), taskpool: taskpool)
template awaitSpawn*[T](jobs: JobQueue[T], exp: untyped): T =
jobs.taskpool.spawn(exp)
await wait(jobs.queue)

40
tests/tjobs.nim Normal file
View File

@ -0,0 +1,40 @@
import std/os
import chronos
import chronos/threadsync
import chronos/unittest2/asynctests
import taskpools
import apatheia/queues
import apatheia/jobs
## todo: setup basic async + threadsignal + taskpools example here
##
proc addNums(a, b: float): float =
os.sleep(500)
echo "adding: ", a, " + ", b
return a + b
proc addNums(queue: SignalQueue[float], a, b: float) =
let res = addNums(a, b)
discard queue.send(res)
suite "async tests":
# var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.
# var queue = newSignalQueue[float]()
var jobs = newJobQueue[float]()
asyncTest "test":
echo "\nstart"
let res = jobs.awaitSpawn addNums(jobs.queue, 1.0, 2.0)
# await sleepAsync(100.milliseconds)
echo "result: ", res.repr
# echo "\nRES: ", args.value
check true