我正在使用一个程序包通过称为pyhaystack的其余API(链接到readthedocs)访问楼宇自动化系统,并且我在使用Tornado IO创建一些异步代码方面获得了一些帮助。我经常遇到API死锁的问题,希望Tornado IO可以提供帮助,否则asyncio
无论如何,有人可以给我提示解决该语法错误的麻烦吗?
File "<ipython-input-3-62580fca3488>", line 51
if res instanceof AsynchronousException:
^
SyntaxError: invalid syntax
这是下面未经测试的脚本的完整版本,语法错误在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()
答案 0 :(得分:0)
这有效if type(res) == AsynchronousException:
:
def _on_response(res):
try:
if type(res) == AsynchronousException:
# This is an error
# res isinstance AsynchronousException:
res.reraise()
else:
# This is our sensor value
print(res)
except:
traceback.print_exc()
finally:
waiting.discard(n)