This commit is contained in:
Jaremy Creechley 2024-02-14 21:49:05 -07:00
parent 1a54fc42c1
commit 17576f94e2
2 changed files with 7 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import chronos
import chronicles import chronicles
export queues export queues
export chronicles
logScope: logScope:
# Lexical properties are typically assigned to a constant: # Lexical properties are typically assigned to a constant:
@ -40,16 +41,18 @@ proc processJobs*[T](jobs: JobQueue[T]) {.async.} =
## This processor waits for events from the queue in the JobQueue ## This processor waits for events from the queue in the JobQueue
## and complete the associated futures. ## and complete the associated futures.
const tn: string = $(JobQueue[T])
info "Processing jobs in job queue for type ", type=tn
while jobs.running: while jobs.running:
info "Processing jobs in job queue"
let res = await(jobs.queue.wait()).get() let res = await(jobs.queue.wait()).get()
debug "got job result", jobResult = res trace "got job result", jobResult = $res
let (id, ret) = res let (id, ret) = res
var fut: Future[T] var fut: Future[T]
if jobs.futures.pop(id, fut): if jobs.futures.pop(id, fut):
fut.complete(ret) fut.complete(ret)
else: else:
raise newException(IndexDefect, "missing future: " & $id) raise newException(IndexDefect, "missing future: " & $id)
info "Processing jobs in job queue"
proc createFuture*[T](jobs: JobQueue[T], name: static string): (JobResult[T], Future[T]) = proc createFuture*[T](jobs: JobQueue[T], name: static string): (JobResult[T], Future[T]) =
let fut = newFuture[T](name) let fut = newFuture[T](name)

View File

@ -13,7 +13,7 @@ import apatheia/tasks
proc addNums(a, b: float): float {.asyncTask.} = proc addNums(a, b: float): float {.asyncTask.} =
os.sleep(100) os.sleep(100)
echo "adding: ", a, " + ", b # info "adding: ", a=a, b=b
return a + b return a + b
proc addNumValues(vals: openArray[float]): float {.asyncTask.} = proc addNumValues(vals: openArray[float]): float {.asyncTask.} =
@ -21,7 +21,7 @@ proc addNumValues(vals: openArray[float]): float {.asyncTask.} =
result = 0.0 result = 0.0
for x in vals: for x in vals:
result += x result += x
echo "adding sums: ", vals # echo "adding sums: ", vals
suite "async tests": suite "async tests":
var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads. var tp = Taskpool.new(num_threads = 2) # Default to the number of hardware threads.