Python Forum
asyncio calls within sync function ( Websocket on_open)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
asyncio calls within sync function ( Websocket on_open)
#1
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
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with pymodbus - ModuleNotFoundError: No module named 'pymodbus.client.sync' stsxbel 2 23,600 Nov-02-2023, 08:20 AM
Last Post: South_east
  asyncio not being faster than synchronous calls mikisDW 0 725 Sep-26-2022, 09:14 AM
Last Post: mikisDW
  How to send a pong on websocket-client tomtom 0 3,628 Aug-15-2022, 05:58 AM
Last Post: tomtom
  Can't write to .txt after opening websocket gerald 5 1,575 May-03-2022, 12:43 PM
Last Post: deanhystad
  Help with WebSocket reading data from anoter function korenron 0 1,328 Sep-19-2021, 11:08 AM
Last Post: korenron
  Calls to Attributes of a Class SKarimi 3 3,377 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  Auto re-pair / re-sync Controller via Script? User3000 2 2,310 Nov-30-2020, 11:42 AM
Last Post: User3000
  list call problem in generator function using iteration and recursive calls postta 1 1,892 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,136 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Pycharm sync PTPeter 1 1,941 Apr-22-2020, 03:50 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020