Created
November 6, 2012 21:22
-
-
Save thezerobit/4027654 to your computer and use it in GitHub Desktop.
Use green-threads to build a "blocking" interface for an asynchronous library.
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
;; you'll need to clone green-threads and cl-async into ~/quicklisp/local-projects | |
(ql:quickload "green-threads") | |
(ql:quickload "cl-async") | |
(defun f-http-client (url &rest rest) | |
(let ((http-future (green-threads:make-future))) | |
(apply #'cl-async:http-client | |
url | |
#'(lambda (status headers body) | |
(green-threads:complete-future | |
http-future status headers body nil)) | |
#'(lambda (err) | |
(green-threads:complete-future | |
http-future nil nil nil err)) | |
rest) | |
http-future)) | |
(cl-async:start-event-loop | |
(lambda () | |
(green-threads:with-green-thread | |
(let ((f1 (f-http-client "http://blog.thezerobit.com/" :timeout 5)) | |
(f2 (f-http-client "http://blog.deliciousrobots.com/" :timeout 5))) | |
(multiple-value-bind | |
(status headers body err) (green-threads:wait-on f1) | |
(when (not err) | |
(format t "Status: ~a bytes: ~a Headers: ~a~%" | |
status (length body) (length headers)))) | |
(multiple-value-bind | |
(status headers body err) (green-threads:wait-on f2) | |
(when (not err) | |
(format t "Status: ~a bytes: ~a Headers: ~a~%" | |
status (length body) (length headers)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment