digital-fabric / affect Goto Github PK
View Code? Open in Web Editor NEWAlgebraic effects for Ruby
License: MIT License
Algebraic effects for Ruby
License: MIT License
Affect.capture
doesn't seem thread-safe because it mutates the current context (@@current
) class variable and Affect
is shared across threads.
For example:
t1 = Thread.new {
Affect.capture(log: -> (message) { puts "logging 1: #{message}" }) do
Affect.perform(:log, "foo")
end
}
t2 = Thread.new {
Affect.capture(log: -> (message) { puts "logging 2: #{message}" }) do
Affect.perform(:log, "bar")
end
}
t1.join
t2.join
might end up printing "logging 2: foo\nlogging 2: bar\n"
instead of "logging 1: foo\nlogging 2: bar\n"
.
This specific issue can be fixed by using Thread.current[:current_ctx]
to attach the current context to the current thread (do you want me to open a PR?). However: I think this will lead to other issues when using Thread
s in user-land [1].
So I looked into using the fiber implementation which doesn't mutate global state, so the above code works fine. I believe it has the same problem when using Thread.new
around perform
as the Thread.current
solution, though. Fibers don't seem to play nicely with threads.
Do you have any experience using your affect
library in a multi-threaded environment? Maybe it's best to simply ban threads from user-land?
[1] The part of a code base which only cares about perform
, not about how to handle the effects.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.