-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexample_async.py
More file actions
41 lines (31 loc) · 1.25 KB
/
example_async.py
File metadata and controls
41 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from lightwave2 import lightwave2
import logging
import asyncio
USER = None
PASSWORD = None
logging.basicConfig(level=logging.DEBUG)
user = USER if USER else input("Enter username: ")
password = PASSWORD if PASSWORD else input("Enter password: ")
#Define a callback function. The callback receives 4 pieces of information:
#feature, feature_id, prev_value, new_value
def alert(**kwargs):
print("Callback received: {}".format(kwargs))
async def main():
#Following three lines are minimal requirement to initialise a connection
#This will start up a background consumer_handler task that will run as long as the event loop is active
#This will keep the states synchronised with the real world, and reconnect if the connection drops
link = lightwave2.LWLink2(user, password)
await link.async_connect()
await link.async_get_hierarchy()
#Following is optional, the background task will call the callback function where a change of state is detected
await link.async_register_callback(alert)
#Dummy main program logic
async def process_loop():
n = 1
while True:
print(f"{n}")
await asyncio.sleep(2)
n = n+1
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.run_until_complete(process_loop())