2021/04/18
Two concurrency utilities
Since I haven't written this blog for a while, it's a good time to catch up recent changes in Gauche HEAD.
Lately, we added a couple of utility modules that help to write concurrent programs.
control.future (draft:control.future) - A typical future,
that is, evaluate the given expression in a separate thread concurrently.
The result can be retrieved with future-get
.
(use control.future) (use rfc.http) (let1 f (future (http-get "example.com" "/")) ... some computation ... (receive (code headers body) (future-get f) ...))
Like most of other synchronization operations in Gauche, future-get
can take timeout parameter.
control.cseq (draft:control.cseq) - It's a lazy sequence but the data generator runs in a separate thread. It can abstract producer-consumer type concurrency.
The same concurrency can be achieved with <mtqueue>
(ref:data.queue), and in fact control.cseq
uses
mtqeuee internally, but cseq is characteristic that you can
very easily change lazy sequence code into concurrent code.
(generator->lseq gen)
returns an lseq, which looks like an
ordinary list, but when you walk down its cdr
, the generator
gen
is called to generate more items.
(generator->cseq gen)
also returns an lseq. But in this case,
gen
runs concurrently in a separate thread. In many cases,
you can simply replace lseq
to cseq
to get the benefit
of concurrency.
The module also provides coroutine->cseq
, which uses coroutine
to generate the items, run in a separate thread.
Tags: 0.9.11, Concurrency
Post a comment