Last active
March 19, 2020 03:34
-
-
Save torus/8ce22c67e6ae93286d92a742f481afba to your computer and use it in GitHub Desktop.
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
| (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 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
| version: '3' | |
| services: | |
| gosh: | |
| image: practicalscheme/gauche | |
| volumes: | |
| - .:/work | |
| working_dir: /work |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
queryは非同期に応答を返す何かのシミュレーション。on-dataとon-endはデータを受け取った時に呼び出されるハンドラ。query-contが継続を使ったラッパー。