1

I want to implement a check if import pdblp is active and if not exit the session.

I note from this link (Bloomberg Anywhere + pdblp or xbbg + not logged in) that a session:

  1. remains logged in for 3 days.
  2. is logged out if a session is opened on another pc.

Therefore, i want to implement a try-execpt block like this:

import pdblp


# check if connected
try:
    con = pdblp.BCon(timeout=5000)
    con.start()
except Exception as e:
    print('not logged in:', e)

my question is, would the above be sufficient to validate the connection ? (ie. would the above throw an error, e).

D.L
  • 4,339
  • 5
  • 22
  • 45
  • If you start a session when the terminal is not connected the API return a message that says it could not connect. I am not familiar with the python API so I don't know if it will parse the message and throw an exception or not. It should not be too difficult to test. – assylias May 06 '22 at 16:44
  • So you have a running Python app and want to see if the Bloomberg connection has dropped after the initial connection at startup? – DS_London May 07 '22 at 09:15
  • @DS_London, yes that is correct. in fact, i want to *guarantee* connection (or exit) before trying to pull data otherwise there is risk of returning empty data and therefore generating a blank dataframe or csv later in the process. – D.L May 07 '22 at 09:42
  • If you use the base-level Bloomberg Python api, you have to write code to handle the responses to requests, whereas wrappers like xbbg do this for you. Using the low-level api gives you much richer error data, and also lets you see “system” events (such as SESSION_STATUS messages). Hence at the low level you can check the message queue for any system messages before making a request. Alternatively just try making a request for a small amount of reference data for a ticker you know exists. https://data.bloomberglp.com/professional/sites/10/2017/03/BLPAPI-Core-Developer-Guide.pdf#page26 – DS_London May 07 '22 at 18:41
  • I am not requesting data from a ticker, so this approach would not work. I am requesting a list from a `BSRCH()` function. So if `not connected`, there is a risk that an empty list would be returned. I therefore want to verify `if connected` or `if not connected` very specifically. – D.L May 07 '22 at 18:50

1 Answers1

0

TL;DR Use the newer blp package instead of blpapi, which is no longer supported by the creator.

pip install blp

try:
    from blp import blp  
    con = blp.BlpQuery().start()# change debug to true to see issues
    
except:
    print('NO BLOOMBERG')

Yes, your try-except is sufficient. The except statement will throw you the error to know that the Bloomberg connection is not working (the link you included to the other SO article correctly noted that the python API will only work under the same conditions that the Excel API does for Bloomberg).

However, it was troublesome for me that con = pdblp.BCon(timeout=5000) con.start() would try to connect for nearly 1 minute. The new blp package will kick out an error in 17 seconds. Just change your con to the new .start()

GoPackGo
  • 341
  • 5
  • 9
  • Agreed that there is a newer package from the creator, but stuck with the old module until (or if) the IT department upgrades to this which could be some time. I actually though that `timeout=5000` would limit connection attempt to 5 seconds (5000 milliseconds). – D.L Jul 19 '22 at 08:56
  • 1
    I thought so too, but it does not appear to work that way. If the pdblp package is not installed, you will get an error right away (perhaps obvious). But if pdblp is installed in your environment, it will start to start the connection for some amount of time (which seems unrelated to the "timeout" parameter, which is used once you make a call) – GoPackGo Jul 19 '22 at 20:45
  • interesting, I removed the `timeout=5000` and get the same, so that observation is correct. In fact there is a default setting in the function definition itself `timeout=500`. – D.L Jul 20 '22 at 10:06