or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
Event Driven Concurrency using the Ruby Fiber Scheduler - Samuel Williams
tags:
COSCUP2021
en
COSCUP2021
RubyConf Taiwan 2021
TR214 - Ruby Conf
歡迎來到 https://hackmd.io/@coscup/2021 共筆
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →點擊本頁上方的 開始用 Markdown 一起寫筆記!
手機版請點選上方 按鈕展開議程列表。
Slido: https://app.sli.do/event/znplkhad
Please feel free to help translate this document.
Background
RubyDNS
is a DNS client and server library for Ruby.EventMachine
is a library for implementing scalable non-blocking programs for Ruby.Celluloid
is a framework for Ruby actor-based concurrency which includes support for non-blocking I/O.Celluloid::DNS
was a fork of RubyDNS implemented on top of Celluloid.The Ruby fiber scheduler interface is specified in this document.
The CRuby implementation of the interface is in
scheduler.c
. The public interface (for use in C extensions, etc) is given inscheduler.h
.Alternative Implementations
These are some other implementations (other than Async) of the fiber scheduler interface:
Socketry
Socketry is an organisation for Async and related event-driven concurrency libraries for Ruby.
Async
Async is a composable asynchronous I/O framework for Ruby. There are currently two branches:
stable-v1
(for Ruby 2.x+) andmain
(for Ruby 3.x+). Currently, Async 2.0 (main
) is not released yet due to CRuby bugs, but will be released at the latest by the end of the year with Ruby 3.1.0, and hopefully sooner with the release of Ruby 3.0.3.Sleep Sort Example
Event
Event is a low level event readiness selector.
As discussed, there are 4 implementations:
Event::Selector::Select
Event::Selector::KQueue
Event::Selector::EPoll
Event::Selector::URing
If you would like to read more about
io_uring
, Linux Weekly News has a good overview.IO Read/Write Example
Falcon
Falcon is an event-driven web server built on top of Async.
99 Bottles of Beer on the Wall
This example shows live streaming through a Rack application running on Falcon. It uses a special
Async::HTTP::Body::Writable
which can be used to send chunks of data from one task to the HTTP response.Source code: https://github.com/socketry/utopia-falcon-heroku/blob/master/pages/beer/controller.rb
Conway's Game of Life
This example shows live client-server interaction with a application running in real time. The server state is synchronised to the client using a WebSocket.
This is the first time I tried to deploy such an application to Heroku, and I found some problems:
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →In any case, it's an interesting demo, showing the possibilies of event-driven concurrency in a web application.
You can make glider like this:
Then press "Start" button.
Feel free to add other fun shapes you find.
Source code: https://github.com/socketry/lively-falcon - you can run it locally too.
Q & A session
Async::Debug
. It's a live debugger and can be used in any async program. It is currently quite simple, but I have some ideas to make it more useful for debugging live systems. Concurrency can be quite tricky, and so we need better debugging tools such as this. The idea originally came from a build tool I created calledteapot
. I wanted to visualise the build graph, and all the build logs, and dependencies, etc. I think the same approach makes sense for concurrent systems in general.Process.detach(pid)
already non-blocking? In my answer I got a bit confused betweendetach
anddaemonize
- I thought it was the same but it's not.Process.detach
creates a thread which waits on the child process. In a way, all child process is non-blocking. But even this thread, if you calljoin
on it, it will block (but we support non-blockingThread#join
in the fiber scheduler). In the fiber scheduler interface, we introduced a new hook calledprocess_wait
which makesProcess.wait
yield to the event loop. In theio_uring
implementation of the event gem, we usepidfd
to wait on the process using the event loop, so it's pretty efficient and non-blocking. More efficient than creating a thread per child process. RegardingProcess.daemonize
you should avoid this kind of model (including, more generally, fork) and useAsync::Container
.HTTPS
by default. You can see the full documentation here which explains how to use it, and some example site here. If you are using ActiveRecord, you might have issues as ActiveRecord only allows one connection per thread (which is problematic as the entire event loop runs on one thread). If you want to try a completely event driven database driver, you could consider db. Sequel also has some partial support for event-driven concurrency.Async::Task#wait
propagates the exception. For network servers, the above design isolations failures to the inner most "task per request". The server itself is generally robust.mmap
buffers (3) no encoding and (4) page aligned (without trailing\0
chracter forced by Ruby). I did introduce some basicpack
/unpack
for binary data and it was 4x faster thanString#unpack
so it would be exciting for binary formats as performance could be a lot better.If you have any other questions feel free to add them below.
n. Your Question …