Skip to content

observeOn doesn't behave as expected (Clojure) #625

Closed
@ghost

Description

(ns rx.lang.clojure.examples.rx-examples
  (:require [rx.lang.clojure.interop :as rx])
  (:import rx.Observable
           rx.subscriptions.Subscriptions
           rx.schedulers.Schedulers
           java.util.concurrent.TimeUnit
))

(println (Thread/currentThread)) ; (*)
(-> (Observable/interval 100 TimeUnit/MILLISECONDS #_(Schedulers/currentThread)) 
    (.take 5)
    (.observeOn (Schedulers/currentThread))
    (.subscribe (rx/action [v]
                             ; this file is created, but the content do not match (*)
                            (spit "/tmp/tid.txt" (Thread/currentThread)) 
                           (println v))) ; this output never reaches the repl
    )

So, Observable/interval spawns a new thread and the subscribed callback is invoked in that thread by default. I need to use observeOn to ensure the cb
is invoked in the current thread so that the println output makes it to the repl.

/tmp/tid.txt does get created, so the cb is called but the println output
never makes it to the repl.
If i schedule Observable/interval in the current thread (commented out) the
output is visible (though the repl hangs afterwards for some reason), but
the observeOn itself doesn't seem to do the job it's meant to.
The contents of /tmp/tid/txt don't match the current thread reported
by the println either.

Is this a bug or am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions