Skip to content

Instantly share code, notes, and snippets.

@thezerobit
Created November 6, 2012 21:22
Show Gist options
  • Save thezerobit/4027654 to your computer and use it in GitHub Desktop.
Save thezerobit/4027654 to your computer and use it in GitHub Desktop.
Use green-threads to build a "blocking" interface for an asynchronous library.
;; 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