Skip to content

Instantly share code, notes, and snippets.

@scymtym
Last active February 12, 2022 19:52
Show Gist options
  • Save scymtym/f893925023407619f556adc777fcf1eb to your computer and use it in GitHub Desktop.
Save scymtym/f893925023407619f556adc777fcf1eb to your computer and use it in GitHub Desktop.
Task handler abuse
(define-condition get-context (condition)
())
(lparallel:task-handler-bind ((get-context (lambda (c)
(declare (ignore c))
(invoke-restart 'receive :my-context))))
(lparallel:pdotimes (i 5)
(print (restart-case (signal 'get-context) (receive (value) value)))))
;;; Shoud be used around sumitting tasks (include pdotimes and friends)
(defmacro with-context ((context) &body body)
`(lparallel:task-handler-bind
((get-context (lambda (c)
(declare (ignore c))
(invoke-restart 'receive ,context))))
,@body))
;;; Can be called in tasks (in wokers)
(defun get-context ()
(restart-case
(signal 'get-context)
(receive (value)
value)))
(with-context (:my-context) (lparallel:pdotimes (i 5) (print (get-context))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment