From: "ko1 (Koichi Sasada)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105242] [Ruby master Feature#18127] Ractor-local version of Singleton
Date: Tue, 14 Sep 2021 05:44:17 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-93649.20210914054417.51834@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-18127.20210823215905.51834@ruby-lang.org>
Issue #18127 has been updated by ko1 (Koichi Sasada).
code: https://github.com/ruby/singleton/pull/4/files
----------------------------------------
Feature #18127: Ractor-local version of Singleton
https://bugs.ruby-lang.org/issues/18127#change-93649
* Author: rm155 (Rohit Menon)
* Status: Open
* Priority: Normal
----------------------------------------
**Background**
When the Singleton module (from the Singleton library) is included in a class, that class will have only one instance. Since the instance can only be in one Ractor at once, Singleton is not Ractor-compatible. For example, the following code would fail upon trying to access Example.instance in the Ractor:
``` ruby
class Example
def initialize
@value = 1
end
end
Example.include Singleton
Ractor.new do
Example.instance
end.take
#=> can not access instance variables of classes/modules from non-main Ractors (Ractor::IsolationError)
```
In some cases, this may be the desired behavior, as it may be important that the class truly have only one instance. However, in many other cases, it would be more convenient for the class to have one instance per Ractor.
**Proposal**
The proposal is to create a RactorLocalSingleton module that can be included instead of Singleton to make the instance Ractor-local.
Here is how RactorLocalSingleton might be used in the situation above:
``` ruby
class Example
def initialize
@value = 1
end
end
Example.include RactorLocalSingleton
Ractor.new do
Example.instance
end.take
```
**Discussion**
The advantage of creating RactorLocalSingleton is that classes could have Singleton-like behavior while being usable in Ractors. Since some libraries, such as Prime, currently rely on the Singleton module, this would enable those libraries to have more flexibility with Ractors.
The disadvantage of creating this module is that it supports the continued use of the Singleton design pattern, which is sometimes considered harmful. An alternative to RactorLocalSingleton might be to simply use Thread-local variables as Singleton instances. Here is how Thread-local variables might be used in the given situation:
``` ruby
class Example
def initialize
@value = 1
end
end
Ractor.new do
Thread.current[:Example] = Example.new
Thread.current[:Example]
end.take
```
**Summary**
Classes that include Singleton are currently incompatible with Ractors. By instead including a new module RactorLocalSingleton, classes can have Singleton-like properties while being used in Ractors. However, this may perpetuate the use of the Singleton design pattern, and using Thread-local variables may be a preferable solution.
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2021-09-14 5:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-23 21:59 [ruby-core:105056] " rm155 (Rohit Menon)
2021-09-14 5:44 ` ko1 (Koichi Sasada) [this message]
2021-09-14 5:45 ` [ruby-core:105243] " ko1 (Koichi Sasada)
2021-09-16 7:21 ` [ruby-core:105289] " matz (Yukihiro Matsumoto)
2021-11-09 1:19 ` [ruby-core:105978] " ioquatix (Samuel Williams)
2021-11-09 14:45 ` [ruby-core:105989] " Dan0042 (Daniel DeLorme)
2024-10-03 6:43 ` [ruby-core:119420] " hsbt (Hiroshi SHIBATA) via ruby-core
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-93649.20210914054417.51834@ruby-lang.org \
--to=noreply@ruby-lang.org \
--cc=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).