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;








5















(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?










share|improve this question


























  • 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


















5















(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?










share|improve this question


























  • 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














5












5








5








(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?










share|improve this question
















(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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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

















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













1 Answer
1






active

oldest

votes


















1














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))








share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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))








    share|improve this answer





























      1














      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))








      share|improve this answer



























        1












        1








        1







        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))








        share|improve this answer













        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))









        share|improve this answer












        share|improve this answer



        share|improve this answer










        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





















            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.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현