-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
from-promise fails on JVM #32
Comments
I was considering writing something for this, but after I read an unimplemented proposal for Clojure promises, I wondered if the |
The main problem of clojure promises, is that they are blocking and does not allow chain comuptations. This means that if you want to convert that promise to observable you will need a dedicated thread to block/wait until promise is resolved, and then emit that value to the observable. This is because is completely notrecommeded work with clojure promises and convert them to observables. |
One way to interface with promises would be to use the In my particular case, http-kit provides an async client that delivers results via a promise. This allows http-kit to run the pool of threads which service the sockets, and provides a simple interface to the eventual delivery. This is a clean interface to an async library, so users of the library can either block or not block depending on their application. So I think the capability to directly use promises on the JVM would be a useful addition to beicon. One way to make that work would be to have a background thread devoted to polling any promises that had turned into observables by using a call to It may be that an object representing this "subscription" to a promise is then easily regarded as a disposable. Calling Another implementation might be to just make a periodic timer observable that polls the promise and returns a single value when it is realized. That might be an easier implementation, but I am more uncertain how a lot of timers inside rx/java will perform. |
All implementations will imply block a thread for some perior of time in order to deref the promise or polling it in a interval if it is realized. That is very inneficient; unfortunatelly if the library you are using return clojure promises you have no choice... If you want composable promises, look at Obviously I think we need to implement the |
This is a good discussion for me. I already do handle this interface via callbacks and The promise was the blocking interface that I would use inside the flowable's generate function. In that context, the blocking is desired, since that is what the generate function and the use case are about. The interesting discussion here distracted me from the original use apparently. But for the blocking case, http-kit's client returns a promise. This gives the caller the flexibility to choose when to block. Looking at the original error message then, if the promise implemented the future interface, the correct behavior would actually occur. In fact, one could argue that would be a nice feature for http-kit to provide, to return something that implements the future interface. However, I think a helper that wrapped promises with the future interface would be all that is actually required. Just thinking out loud here: (defn promise->future [p]
(reify java.util.concurrent.Future
(cancel [this _] false)
(get [this] (deref p))
(get [this tout unit]
(deref p (.convert java.util.concurrent.TimeUnit/MILLISECONDS unit tout) :timeout))
(isCancelled [this] false)
(isDone [this] (realized? p))) Would such a thing be sufficient for the clj implementation for beicon. On the JVM, promises and futures are inherently blocking. It may not be performant, but it might be exactly what a user is trying to achieve. Not sure if I believe my own argument here, but I am putting it out for further discussion. |
The response is: yes, it should be enough with that. But on the jvm, the futures are not inherently blocking, let see https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html (that impl is used in Blocking on |
I think that is a reasonable perspective. Given that, should from-promise be removed from the JVM implementation of the library? Anyone coming from javascript or a language with true async behavior for their promises will be surprised. |
I guess alternatively, it could be renamed |
renamed to |
Clojure 1.8, JRE 1.8, beicon 3.5.0
The text was updated successfully, but these errors were encountered: