Last active
February 12, 2022 19:52
-
-
Save scymtym/f893925023407619f556adc777fcf1eb to your computer and use it in GitHub Desktop.
Task handler abuse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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