Zio run blocking backwardscompatible codeIs there potential starvation in this code or is it just me?Why won't astyanax (java) recognize my @Id annotated values in my scala case class parameter list?Pass function argument as code blockPartial Functions and Execute-Once behaviour in ScalaCustomising composition of Future, Either and Writer in ScalazWriting good Scala without co-routines (including a Python example using yield)Wrap asynchronous code in a blocking callAssignment of code block into ValScala Thunk or Code BlockIs this usage of a HashMap thread safe?
(Why) May a Beit Din refuse to bury a body in order to coerce a man into giving a divorce?
Why would the US President need briefings on UFOs?
How would one country purchase another?
Something in the TV
Starships without computers?
Can pay be witheld for hours cleaning up after closing time?
Is it insecure to have an ansible user with passwordless sudo?
Was Tuvok bluffing when he said that Voyager's transporters rendered the Kazon weapons useless?
The sound of thunder's like a whip
Was 'help' pronounced starting with a vowel sound?
Can you grapple/shove with the Hunter Ranger's Whirlwind Attack?
What does it mean to have a subnet mask /32?
Importing ES6 module in LWC project (sfdx)
Does adding the 'precise' tag to daggers break anything?
How to specify and fit a hybrid machine learning - linear model
Are there nouns that change meaning based on gender?
The logic of invoking virtual functions is not clear (or it is method hiding?)
Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner
Why didn’t Doctor Strange stay in the original winning timeline?
Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?
What can I do to keep a threaded bolt from falling out of its slot?
Do I have to learn /o/ or /ɔ/ separately?
Turn TDE off when restoring SQL databases
Why are delta bots so finicky?
Zio run blocking backwardscompatible code
Is there potential starvation in this code or is it just me?Why won't astyanax (java) recognize my @Id annotated values in my scala case class parameter list?Pass function argument as code blockPartial Functions and Execute-Once behaviour in ScalaCustomising composition of Future, Either and Writer in ScalazWriting good Scala without co-routines (including a Python example using yield)Wrap asynchronous code in a blocking callAssignment of code block into ValScala Thunk or Code BlockIs this usage of a HashMap thread safe?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
(Hopefully) simple question on Scalaz Zio.
I have some old code that I refactored to Zio. I want one path of that code to keep behaving exactly as it was:
- synchronous
- blocking
- on the current thread (this is a hard requirement)
How can I run an IO
such that it behaves like the old blocking code?
I currently use:
private lazy val blockingRts = new RTS
def runBlocking[E, A](io: IO[E, A]): Either[E, A] =
blockingRts.unsafeRun(io.attempt)
It seems to do the trick, but I am far from sure that this is correct. Is this 100% backwards compatible with the old code?
scala scalaz-zio
add a comment |
(Hopefully) simple question on Scalaz Zio.
I have some old code that I refactored to Zio. I want one path of that code to keep behaving exactly as it was:
- synchronous
- blocking
- on the current thread (this is a hard requirement)
How can I run an IO
such that it behaves like the old blocking code?
I currently use:
private lazy val blockingRts = new RTS
def runBlocking[E, A](io: IO[E, A]): Either[E, A] =
blockingRts.unsafeRun(io.attempt)
It seems to do the trick, but I am far from sure that this is correct. Is this 100% backwards compatible with the old code?
scala scalaz-zio
Especially the requirement to have it run on the current thread sounds very shady to me. This seems like a piece of code that - from a functional perspective - absolutely does not belong into anIO
. But that's just my gut feeling.
– Markus Appel
Apr 1 at 13:48
That aside - maybe Sync is what you're looking for?
– Markus Appel
Apr 1 at 14:30
add a comment |
(Hopefully) simple question on Scalaz Zio.
I have some old code that I refactored to Zio. I want one path of that code to keep behaving exactly as it was:
- synchronous
- blocking
- on the current thread (this is a hard requirement)
How can I run an IO
such that it behaves like the old blocking code?
I currently use:
private lazy val blockingRts = new RTS
def runBlocking[E, A](io: IO[E, A]): Either[E, A] =
blockingRts.unsafeRun(io.attempt)
It seems to do the trick, but I am far from sure that this is correct. Is this 100% backwards compatible with the old code?
scala scalaz-zio
(Hopefully) simple question on Scalaz Zio.
I have some old code that I refactored to Zio. I want one path of that code to keep behaving exactly as it was:
- synchronous
- blocking
- on the current thread (this is a hard requirement)
How can I run an IO
such that it behaves like the old blocking code?
I currently use:
private lazy val blockingRts = new RTS
def runBlocking[E, A](io: IO[E, A]): Either[E, A] =
blockingRts.unsafeRun(io.attempt)
It seems to do the trick, but I am far from sure that this is correct. Is this 100% backwards compatible with the old code?
scala scalaz-zio
scala scalaz-zio
edited Mar 27 at 17:26
Lodewijk Bogaards
asked Mar 27 at 15:24
Lodewijk BogaardsLodewijk Bogaards
16.3k2 gold badges21 silver badges44 bronze badges
16.3k2 gold badges21 silver badges44 bronze badges
Especially the requirement to have it run on the current thread sounds very shady to me. This seems like a piece of code that - from a functional perspective - absolutely does not belong into anIO
. But that's just my gut feeling.
– Markus Appel
Apr 1 at 13:48
That aside - maybe Sync is what you're looking for?
– Markus Appel
Apr 1 at 14:30
add a comment |
Especially the requirement to have it run on the current thread sounds very shady to me. This seems like a piece of code that - from a functional perspective - absolutely does not belong into anIO
. But that's just my gut feeling.
– Markus Appel
Apr 1 at 13:48
That aside - maybe Sync is what you're looking for?
– Markus Appel
Apr 1 at 14:30
Especially the requirement to have it run on the current thread sounds very shady to me. This seems like a piece of code that - from a functional perspective - absolutely does not belong into an
IO
. But that's just my gut feeling.– Markus Appel
Apr 1 at 13:48
Especially the requirement to have it run on the current thread sounds very shady to me. This seems like a piece of code that - from a functional perspective - absolutely does not belong into an
IO
. But that's just my gut feeling.– Markus Appel
Apr 1 at 13:48
That aside - maybe Sync is what you're looking for?
– Markus Appel
Apr 1 at 14:30
That aside - maybe Sync is what you're looking for?
– Markus Appel
Apr 1 at 14:30
add a comment |
1 Answer
1
active
oldest
votes
Okay, I finally looked under the hood and implemented something that seems to be fulfilling my requirements:
/**
* Executes the IO synchronous and blocking on the current thread, thus running an IO
* without any of the advantages of IO. This can be useful for maintaining backwards compatibility.
* Rethrows any exception that was not handled by the IO's error handling.
*/
@throws
def runLegacy[E, A](io: IO[E, A]): Either[E, A] =
syncBlockingRunTimeSystem.unsafeRunSync[Nothing, Either[E, A]](io.either) match
case Exit.Success(v) => v
case Exit.Failure(Cause.Die(exception)) => throw exception
case Exit.Failure(Cause.Interrupt) => throw new InterruptedException
case Exit.Failure(fail) => throw FiberFailure(fail)
private lazy val syncBlockingRunTimeSystem = Runtime(
(),
PlatformLive.fromExecutor(new Executor
override def yieldOpCount: Int = Int.MaxValue
override def metrics: Option[ExecutionMetrics] = None
override def submit(runnable: Runnable): Boolean =
runnable.run()
true
override def here: Boolean = true
)
)
I also wrote a couple of tests:
"runLegacy" should
"run synchronous code in blocking fashion on current thread" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
.map _ => runCount +=1
.flatMap _ =>
runCount += 1
IO.effect
runCount += 1
Thread.currentThread()
runCount shouldBe 0
runLegacy(io) shouldBe Right(Thread.currentThread())
runCount shouldBe 4
"run parallel code sequentially on current thread" in
val ios = (1 to 500).map i => IO.succeedLazy i
runLegacy(IO.reduceAll(IO.succeed(0), ios)
case (a, b) => a + b
) shouldBe Right((500 * 501) / 2)
"run many flatMaps without overflowing" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
val manyIo = (1 to 9999).foldLeft(io) case (acc, _) => acc.flatMap _ => io
runLegacy(manyIo)
runCount shouldBe 10000
case object TestException extends Throwable
"handle sync blocking errors" in
case object TestException extends Throwable
runLegacy(IO.effect(throw TestException)) shouldBe Left(TestException)
"rethrow unhandled exceptions" in
assertThrows[TestException.type]
runLegacy(IO.succeedLazy(throw TestException))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380840%2fzio-run-blocking-backwardscompatible-code%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Okay, I finally looked under the hood and implemented something that seems to be fulfilling my requirements:
/**
* Executes the IO synchronous and blocking on the current thread, thus running an IO
* without any of the advantages of IO. This can be useful for maintaining backwards compatibility.
* Rethrows any exception that was not handled by the IO's error handling.
*/
@throws
def runLegacy[E, A](io: IO[E, A]): Either[E, A] =
syncBlockingRunTimeSystem.unsafeRunSync[Nothing, Either[E, A]](io.either) match
case Exit.Success(v) => v
case Exit.Failure(Cause.Die(exception)) => throw exception
case Exit.Failure(Cause.Interrupt) => throw new InterruptedException
case Exit.Failure(fail) => throw FiberFailure(fail)
private lazy val syncBlockingRunTimeSystem = Runtime(
(),
PlatformLive.fromExecutor(new Executor
override def yieldOpCount: Int = Int.MaxValue
override def metrics: Option[ExecutionMetrics] = None
override def submit(runnable: Runnable): Boolean =
runnable.run()
true
override def here: Boolean = true
)
)
I also wrote a couple of tests:
"runLegacy" should
"run synchronous code in blocking fashion on current thread" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
.map _ => runCount +=1
.flatMap _ =>
runCount += 1
IO.effect
runCount += 1
Thread.currentThread()
runCount shouldBe 0
runLegacy(io) shouldBe Right(Thread.currentThread())
runCount shouldBe 4
"run parallel code sequentially on current thread" in
val ios = (1 to 500).map i => IO.succeedLazy i
runLegacy(IO.reduceAll(IO.succeed(0), ios)
case (a, b) => a + b
) shouldBe Right((500 * 501) / 2)
"run many flatMaps without overflowing" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
val manyIo = (1 to 9999).foldLeft(io) case (acc, _) => acc.flatMap _ => io
runLegacy(manyIo)
runCount shouldBe 10000
case object TestException extends Throwable
"handle sync blocking errors" in
case object TestException extends Throwable
runLegacy(IO.effect(throw TestException)) shouldBe Left(TestException)
"rethrow unhandled exceptions" in
assertThrows[TestException.type]
runLegacy(IO.succeedLazy(throw TestException))
add a comment |
Okay, I finally looked under the hood and implemented something that seems to be fulfilling my requirements:
/**
* Executes the IO synchronous and blocking on the current thread, thus running an IO
* without any of the advantages of IO. This can be useful for maintaining backwards compatibility.
* Rethrows any exception that was not handled by the IO's error handling.
*/
@throws
def runLegacy[E, A](io: IO[E, A]): Either[E, A] =
syncBlockingRunTimeSystem.unsafeRunSync[Nothing, Either[E, A]](io.either) match
case Exit.Success(v) => v
case Exit.Failure(Cause.Die(exception)) => throw exception
case Exit.Failure(Cause.Interrupt) => throw new InterruptedException
case Exit.Failure(fail) => throw FiberFailure(fail)
private lazy val syncBlockingRunTimeSystem = Runtime(
(),
PlatformLive.fromExecutor(new Executor
override def yieldOpCount: Int = Int.MaxValue
override def metrics: Option[ExecutionMetrics] = None
override def submit(runnable: Runnable): Boolean =
runnable.run()
true
override def here: Boolean = true
)
)
I also wrote a couple of tests:
"runLegacy" should
"run synchronous code in blocking fashion on current thread" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
.map _ => runCount +=1
.flatMap _ =>
runCount += 1
IO.effect
runCount += 1
Thread.currentThread()
runCount shouldBe 0
runLegacy(io) shouldBe Right(Thread.currentThread())
runCount shouldBe 4
"run parallel code sequentially on current thread" in
val ios = (1 to 500).map i => IO.succeedLazy i
runLegacy(IO.reduceAll(IO.succeed(0), ios)
case (a, b) => a + b
) shouldBe Right((500 * 501) / 2)
"run many flatMaps without overflowing" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
val manyIo = (1 to 9999).foldLeft(io) case (acc, _) => acc.flatMap _ => io
runLegacy(manyIo)
runCount shouldBe 10000
case object TestException extends Throwable
"handle sync blocking errors" in
case object TestException extends Throwable
runLegacy(IO.effect(throw TestException)) shouldBe Left(TestException)
"rethrow unhandled exceptions" in
assertThrows[TestException.type]
runLegacy(IO.succeedLazy(throw TestException))
add a comment |
Okay, I finally looked under the hood and implemented something that seems to be fulfilling my requirements:
/**
* Executes the IO synchronous and blocking on the current thread, thus running an IO
* without any of the advantages of IO. This can be useful for maintaining backwards compatibility.
* Rethrows any exception that was not handled by the IO's error handling.
*/
@throws
def runLegacy[E, A](io: IO[E, A]): Either[E, A] =
syncBlockingRunTimeSystem.unsafeRunSync[Nothing, Either[E, A]](io.either) match
case Exit.Success(v) => v
case Exit.Failure(Cause.Die(exception)) => throw exception
case Exit.Failure(Cause.Interrupt) => throw new InterruptedException
case Exit.Failure(fail) => throw FiberFailure(fail)
private lazy val syncBlockingRunTimeSystem = Runtime(
(),
PlatformLive.fromExecutor(new Executor
override def yieldOpCount: Int = Int.MaxValue
override def metrics: Option[ExecutionMetrics] = None
override def submit(runnable: Runnable): Boolean =
runnable.run()
true
override def here: Boolean = true
)
)
I also wrote a couple of tests:
"runLegacy" should
"run synchronous code in blocking fashion on current thread" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
.map _ => runCount +=1
.flatMap _ =>
runCount += 1
IO.effect
runCount += 1
Thread.currentThread()
runCount shouldBe 0
runLegacy(io) shouldBe Right(Thread.currentThread())
runCount shouldBe 4
"run parallel code sequentially on current thread" in
val ios = (1 to 500).map i => IO.succeedLazy i
runLegacy(IO.reduceAll(IO.succeed(0), ios)
case (a, b) => a + b
) shouldBe Right((500 * 501) / 2)
"run many flatMaps without overflowing" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
val manyIo = (1 to 9999).foldLeft(io) case (acc, _) => acc.flatMap _ => io
runLegacy(manyIo)
runCount shouldBe 10000
case object TestException extends Throwable
"handle sync blocking errors" in
case object TestException extends Throwable
runLegacy(IO.effect(throw TestException)) shouldBe Left(TestException)
"rethrow unhandled exceptions" in
assertThrows[TestException.type]
runLegacy(IO.succeedLazy(throw TestException))
Okay, I finally looked under the hood and implemented something that seems to be fulfilling my requirements:
/**
* Executes the IO synchronous and blocking on the current thread, thus running an IO
* without any of the advantages of IO. This can be useful for maintaining backwards compatibility.
* Rethrows any exception that was not handled by the IO's error handling.
*/
@throws
def runLegacy[E, A](io: IO[E, A]): Either[E, A] =
syncBlockingRunTimeSystem.unsafeRunSync[Nothing, Either[E, A]](io.either) match
case Exit.Success(v) => v
case Exit.Failure(Cause.Die(exception)) => throw exception
case Exit.Failure(Cause.Interrupt) => throw new InterruptedException
case Exit.Failure(fail) => throw FiberFailure(fail)
private lazy val syncBlockingRunTimeSystem = Runtime(
(),
PlatformLive.fromExecutor(new Executor
override def yieldOpCount: Int = Int.MaxValue
override def metrics: Option[ExecutionMetrics] = None
override def submit(runnable: Runnable): Boolean =
runnable.run()
true
override def here: Boolean = true
)
)
I also wrote a couple of tests:
"runLegacy" should
"run synchronous code in blocking fashion on current thread" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
.map _ => runCount +=1
.flatMap _ =>
runCount += 1
IO.effect
runCount += 1
Thread.currentThread()
runCount shouldBe 0
runLegacy(io) shouldBe Right(Thread.currentThread())
runCount shouldBe 4
"run parallel code sequentially on current thread" in
val ios = (1 to 500).map i => IO.succeedLazy i
runLegacy(IO.reduceAll(IO.succeed(0), ios)
case (a, b) => a + b
) shouldBe Right((500 * 501) / 2)
"run many flatMaps without overflowing" in
var runCount = 0
val io = IO.succeedLazy runCount += 1
val manyIo = (1 to 9999).foldLeft(io) case (acc, _) => acc.flatMap _ => io
runLegacy(manyIo)
runCount shouldBe 10000
case object TestException extends Throwable
"handle sync blocking errors" in
case object TestException extends Throwable
runLegacy(IO.effect(throw TestException)) shouldBe Left(TestException)
"rethrow unhandled exceptions" in
assertThrows[TestException.type]
runLegacy(IO.succeedLazy(throw TestException))
answered Apr 1 at 20:52
Lodewijk BogaardsLodewijk Bogaards
16.3k2 gold badges21 silver badges44 bronze badges
16.3k2 gold badges21 silver badges44 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380840%2fzio-run-blocking-backwardscompatible-code%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Especially the requirement to have it run on the current thread sounds very shady to me. This seems like a piece of code that - from a functional perspective - absolutely does not belong into an
IO
. But that's just my gut feeling.– Markus Appel
Apr 1 at 13:48
That aside - maybe Sync is what you're looking for?
– Markus Appel
Apr 1 at 14:30