mirror of
https://github.com/logos-storage/apatheia.git
synced 2026-01-05 22:43:10 +00:00
updates
This commit is contained in:
parent
65a6f4fc77
commit
51459a8c9b
17
src/apatheia/jobs.nim
Normal file
17
src/apatheia/jobs.nim
Normal 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
40
tests/tjobs.nim
Normal 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
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user