Rblp / Rblpapi

R package interfacing the Bloomberg API from https://www.bloomberglabs.com/api/
Other
167 stars 75 forks source link

request for adding unsubscribe function #179

Open davidlamcm opened 8 years ago

davidlamcm commented 8 years ago

forgive me if I miss it. I cannot find an unsubscribe function after I subscribed a security using subscribe().

eddelbuettel commented 8 years ago

True, but do you really need it? E.g. if you were to run a batch job with a subscribe() call, once it ends it just ends. Does the API mandate / n suggest / recommend / mention an unsubscribe action?

davidlamcm commented 8 years ago

it doesnt mandate an unsubscribe action. But what I am planning is sort of combining it with Shiny. There will be multiple requests through out the day to subscribe the datafeed. If I do not kill those no longer used subscription, I am worried it will be a performance hog.

eddelbuettel commented 8 years ago

Maybe, maybe not. That is a conjecture at this point.

We do have shiny apps with long-running db connections. I typically open these at the start and just hold on to them. No leaks, no issues.

eddelbuettel commented 8 years ago

The API documentation mentions unsubscribe but the language used makes it seem like an optional actions. Page 53:

  • Cancellation: Subscriptions (each subscription identified by its correlation ID) can be cancelled by the unsubscribe method of Session

That is can and not should or must.

Also, in src/subscribe.cpp we do not export the session object making it kinda hard to unsubscribe from it. But by existing the scope we get C++ to automatically dispose of it.

Anyway, pull requests welcome as always if you still think this is needed.

meisterluk commented 6 years ago

The current API design makes me wonder. In version 0.3.8, essentially

subscribe(securities=c("TYZ5 Comdty","/cusip/912810RE0@BGN"),
          fields=c("BID"),
          fun=function(x) print(str(x$topic)))

blocks the entire process and waits for any messages. Once they arrive, they will be processed by fun and the waiting continues. The resulting user experience is (IMHO) horrible:

In RStudio, you run the script using subscribe(). After some time, you stop the script by clicking on the red stop icon. After a few seconds, RStudio notifies you that the script crashed and needs to restart the session completely. RStudio restarts, all variables are lost and any changes to the file you made after starting the script.

I can think of the following solutions:

  1. I am not sure what is triggered by the red stop topic. A graceful method to stop listing for subscriptions seems to be triggering Rcpp::internal::InterruptedException (handled by subscribe.cpp line 230), but apparently the red stop icon does not trigger Rcpp::internal::InterruptedException. So we need to find a way to catch its corresponding event.
  2. The user is supposed to run the subscription in a concurrent instance (not sure which, but some parallelization package of R should be appropriate). In the main script some unsubscribe function is called to terminate the concurrent listening. In this case, we need an unsubscribe function and user documentation is required.
  3. subscribe provides a timeout parameter. If no message arrives within the given timeframe, the subscribe function terminates gracefully and stops blocking. I think this a nice-to-have feature.
  4. subscribe provides a message-count parameter. If n messages arrived, the subscribe function terminates gracefully and stops blocking. I think this a nice-to-have feature.

Any thoughts on this? Do you think these proposals are sane?

johnlaing commented 6 years ago

I do not have this problem. You are correct that running subscribe blocks, however when I cancel via ctrl-C in the console I am gracefully returned to the prompt. My session does not crash and I do not lose anything.

Have you tried working in the console? If you have the same issue there we can try to debug further, otherwise it would seem the problem is with RStudio and there's not much we can do to help.

meisterluk commented 6 years ago

Oh, good idea @johnlaing. I will try it within the next 2 weeks and will provide feedback accordingly. Thank you!

meisterluk commented 6 years ago

Hm, I pressed Ctrl+C when the subscription was running, but RStudio seems to completely ignore it (in the console). Not sure, how we can improve the situation.

johnlaing commented 6 years ago

It sounds like you're still in RStudio. When I say "in the console" I mean "outside of Rstudio". Please try that.

meisterluk commented 6 years ago

@johnlaing Thanks for your response. Sorry, I thought you meant "Console" in RStudio. In RStudio no action is taken if Ctrl+C is pressed and the red stop sign seems to be the only option. Today I had the opportunity to test it on cmd.exe. It works perfectly fine. Ctrl+C aborts the session as desired.

image

I think I should take this issue upstream to RStudio. Assuming I want to implement option (1) in my list of alternatives above. Thank you.