Last active
March 19, 2020 03:34
-
-
Save torus/8ce22c67e6ae93286d92a742f481afba to your computer and use it in GitHub Desktop.
This file contains 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
(use gauche.threads) | |
(use data.queue) | |
(define *queue* (make-mtqueue)) | |
(define *cont-queue* (make-mtqueue)) | |
(define (on-data n) | |
(let ((proc (dequeue! *cont-queue* #f))) | |
(if proc | |
(proc n) | |
(enqueue/wait! *queue* (^[] (on-data n)))))) | |
(define (on-end) | |
(let ((proc (dequeue! *cont-queue* #f))) | |
(if proc | |
(proc #f) | |
(enqueue/wait! *queue* on-end)))) | |
(define (query) | |
(let ((th (make-thread | |
(^[] | |
(let loop ((n 10)) | |
(if (> n 0) | |
(begin | |
(thread-sleep! 0.4) | |
(enqueue/wait! *queue* (^[] (on-data n))) | |
(loop (- n 1))) | |
(begin | |
(enqueue/wait! *queue* on-end) | |
(enqueue/wait! *queue* 'done)))))))) | |
(thread-start! th) | |
th)) | |
(define (query-cont) | |
(query) | |
(lambda (yield) | |
(call/cc (lambda (cont) | |
(enqueue! *cont-queue* cont) | |
(yield) | |
)))) | |
(define (app-main) | |
(call/cc (lambda (cont) | |
(let ((handle (query-cont))) | |
(let loop ((sum 0) | |
(n (handle cont))) | |
(if n | |
(begin | |
(print #"Data received ~n") | |
(loop (+ n sum) (handle cont))) | |
(begin | |
(print (* sum sum)) | |
(print "End")))))))) | |
(enqueue/wait! *queue* app-main) | |
(let loop ((task (dequeue/wait! *queue*))) | |
(unless (eq? task 'done) | |
(thread-sleep! 0.5) | |
(task) | |
(loop (dequeue/wait! *queue*)))) |
This file contains 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
version: '3' | |
services: | |
gosh: | |
image: practicalscheme/gauche | |
volumes: | |
- .:/work | |
working_dir: /work |
query
は非同期に応答を返す何かのシミュレーション。
on-data
と on-end
はデータを受け取った時に呼び出されるハンドラ。
query-cont
が継続を使ったラッパー。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/torus/8ce22c67e6ae93286d92a742f481afba/90f62668e5d99bd33794bc5b182de816827883de
オリジナルのコード。