Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

SyntaxError: if res instanceof AsynchronousException:

I am using a package to access a building automation system thru a rest API called pyhaystack , (link to readthedocs) and I am getting some help for using Tornado IO to create some asynchronous code. Ultimetely I am running into issues with the API deadlocking and I hoping Tornado IO can help, else asyncio

Anyway can someone give me a tip to trouble shoot this syntax error? File "<ipython-input-3-62580fca3488>", line 51 if res instanceof AsynchronousException: ^ SyntaxError: invalid syntax

This is the full version of the untested script below, the syntax error is in the if __name__ == '__main__':

from tornado.ioloop import IOLoop
import time
import traceback
import numpy as np
from pyhaystack.client.niagara import Niagara4HaystackSession
from pyhaystack.util.asyncexc import AsynchronousException
import threading

class HaystackThread(object):
    def __init__(self):
        # Create just one session object, used by the thread
        self._client = session = Niagara4HaystackSession(uri='http://10.30.1.27', username='bb', password='Marley123!', pint=True, http_args=dict(debug=True))
        self._io_loop = IOLoop()
        self._thread = None

    def start(self):
        # Create a thread and start the IOLoop within it
        self._thread = threading.Thread(target=self._io_loop.start)

    def stop(self):
        # Stop the IOLoop first by issuing the stop command within the IOLoop thread
        # then waiting for the thread to exit.
        def _stop():
            self._io_loop.stop()
            self._thread = None
        self._io_loop.add_callback(_stop)
        self._thread.join()

    def getState(self, callback):
        def _on_result(operation, **kwargs):
            try:
                callback(operation.result.tags['curVal'])
            except:
                callback(AsynchronousException())
        def _get_state():
            self._client.find_entity(filter_expr='dat2', single=True, callback=_on_result)
        self._io_loop.add_callback(_get_state)


if __name__ == '__main__':
    # Create a thread instance and start it.
    thread = HaystackThread()
    thread.start()

    # Fire off some requests
    waiting = set()
    for n in range(0, 8):
        waiting.add(n)
        def _on_response(res):
            try:
                if res instanceof AsynchronousException:
                    # This is an error
                    res.reraise()
                else:
                    # This is our sensor value
                    print(res)
            except:
                traceback.print_exc()
            finally:
                waiting.discard(n)
        thread.getState(_on_response)

    while waiting:
        time.sleep(1)

    thread.stop()

Comments