I am currently using a Websocket to get data from a SSEClient and send them using Websocket, the difficulty is that I also need to pilot an IO interface within the same function and the library is written with asynio (I am not so familiar with it)
Here is the code I need to implement for accessing IO interface
Here is the code (I made it shorter just keeping relevant instructions) for getting SSE data and sending by Websocket
I define a function to call the asynnc IO interface functions
I tried this:
wrote two async functions
1) one to INIT
Would there any way to do this properly ? by defining an object with would initialise the interface and then do the calls for piloting the IO ? As a begginer I have some difficulties to write such function mixing async and sync and I would need some help.
Here is the code I need to implement for accessing IO interface
async with IPX800(host='IP', api_key='API') as ipx: await ipx.init_config() # Init IO interface await ipx.update_ana(id,value1fromSSE) # update an IO[/python]
Here is the code (I made it shorter just keeping relevant instructions) for getting SSE data and sending by Websocket
def on_open(ws): # when the socket open while True: # infinite loop messages = SSEClient('http://IP/system/events/sse/stream') # open SSE stream ...... I fill a dictionary with the data I got ...... I send the dictionary using the Websocket StrIng=json.dumps(dict) ws.send(StrIng)So currently I have in place the following (and it works)
I define a function to call the asynnc IO interface functions
async def asetANA16(id,value): async with IPX800(host='IP', api_key='API') as ipx: await ipx.init_config() await ipx.update_ana(id,value)I call this function with my Websocket routine under asyncio.run(...)
def on_open(ws): # when the socket open while True: # infinite loop messages = SSEClient('http://IP/system/events/sse/stream') # open SSE stream ...... # I fill a dictionary with the data I got ...... # I send the dictionary using the Websocket asyncio.run( asetANA16(id,value)) # I pilot my IO interface StrIng=json.dumps(dict) ws.send(StrIng)The problem is that I need to initialise everytime the IO interface when it should normaly not be necessary (just one time)
I tried this:
wrote two async functions
1) one to INIT
async def init(): async with IPX800(host='IP', api_key='API') as ipx: await ipx.init_config()2) one to access IO
sync def ANA16(id,value): await ipx.update_ana(id,value)then I insert this into the code
def on_open(ws): # when the socket open asyncio.run(init()) # Init IO interface) while True: # infinite loop messages = SSEClient('http://IP/system/events/sse/stream') ...... # I fill a dictionary with the data I got asyncio.run(ANA16(id,value)) # update of the IO I need to update ...... # I send the dictionary using the Websocket StrIng=json.dumps(dict) ws.send(StrIng)It does not work and my IOs are not updated (I guess because the initalisation (await ipx.init_config()) is local to the function.
Would there any way to do this properly ? by defining an object with would initialise the interface and then do the calls for piloting the IO ? As a begginer I have some difficulties to write such function mixing async and sync and I would need some help.