Project

General

Profile

Actions

Feature #18162

open

Shorthand method Proc#isolate to create isolated proc objects

Added by tagomoris (Satoshi Tagomori) over 2 years ago. Updated over 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:105212]

Description

Currently, isolated proc objects can be created only via Ractor.make_shareable() method.
But isolated proc objects are useful for other use-cases too. So I want to propose a new method Proc#isolate to make it isolated.

For example, it's useful with async I/O patterns like this:

x = 1
y = 2

chk1 = ->(async_io){ async_io.write JSON.dump({x:, y:}) }.isolate
async_make_checkpoint(&chk1)

x = processing_may_fail(x)
y = processing_may_fail(y)

chk2 = ->(async_io){ async_io.write JSON.dump({x:, y:}) }.isolate
async_make_checkpoint(&chk2)

# ...

In the use-case above, async_make_checkpoint is to create a checkpoint in an async manner to record the (lexical) "current" value of variables. If we can do the thing like above, we'll be able to record how values of x/y are changed (or not changed).
In my opinion, we'll have many similar use-cases because Ruby 3.x has the fiber scheduler.

And of course, this should be also useful to define shareable methods using Module#define_method.

x = 1
define_method(:get_x, &->(){ x }.isolate)

# is much simpler than below
x = 1
getter_x = Ractor.make_shareable(->(){ x })
define_method(:get_x, &getter_x)
Actions

Also available in: Atom PDF

Like0
Like0