Skip to content

Instantly share code, notes, and snippets.

@quinnj
Created March 25, 2020 17:03
Show Gist options
  • Save quinnj/40e989a28e3ee0138ff61d00b6d75313 to your computer and use it in GitHub Desktop.
Save quinnj/40e989a28e3ee0138ff61d00b6d75313 to your computer and use it in GitHub Desktop.
Example of background thread workers for spawned tasks
module Workers
const WORK_QUEUE = Channel{Task}(0)
macro async(thunk)
esc(quote
tsk = @task $thunk
# the next line passes along task-local storage to the spawned task
# may not be desirable in all use-cases
tsk.storage = current_task().storage
put!(Workers.WORK_QUEUE, tsk)
tsk
end)
end
function init()
tids = Threads.nthreads() == 1 ? (1:1) : 2:Threads.nthreads()
Threads.@threads for tid in 1:Threads.nthreads()
if tid in tids
Base.@async begin
for task in WORK_QUEUE
schedule(task
wait(task)
end
end
end
end
return
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment