Skip to content

Instantly share code, notes, and snippets.

@torus
Last active March 19, 2020 03:34
Show Gist options
  • Save torus/8ce22c67e6ae93286d92a742f481afba to your computer and use it in GitHub Desktop.
Save torus/8ce22c67e6ae93286d92a742f481afba to your computer and use it in GitHub Desktop.
(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*))))
version: '3'
services:
gosh:
image: practicalscheme/gauche
volumes:
- .:/work
working_dir: /work
@torus
Copy link
Author

torus commented Mar 18, 2020

@torus
Copy link
Author

torus commented Mar 19, 2020

query は非同期に応答を返す何かのシミュレーション。
on-dataon-end はデータを受け取った時に呼び出されるハンドラ。

query-cont が継続を使ったラッパー。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment