I would like to access ARTIQ dataset broadcasts from a custom applet running outside of our ARTIQ-8 Nix environment. Does anyone have experience setting this up with sipyco?

I've tried running the following script

from sipyco.sync_struct import Subscriber
import time
import asyncio

server = '192.168.1.83'
port = 3250

loop = asyncio.new_event_loop()

def sub_init(dat):
    print(dat)
    
def sub_mod(mod):
    print(mod)

subscriber = Subscriber("datasets", sub_init, sub_mod)
loop.run_until_complete(subscriber.connect(server, port))

time.sleep(5)

However the two handler functions do not update when dataset broadcasts are sent out by the master. I have the feeling that I am not taking the right approach here, but I can't seem to find examples where this is done.

If anyone has experience with this I would appreciate any tips/pointers you might have!

    Hi jroth,

    You can try to make the following changes (Reference: artiq_client):

    @@ -6,14 +6,24 @@ server = '192.168.1.83'
     port = 3250
     
     loop = asyncio.new_event_loop()
    +asyncio.set_event_loop(loop)
     
    -def sub_init(dat):
    +dat = dict()
    +
    +def sub_init(x):
    +    dat.clear()
    +    dat.update(x)
         print(dat)
    +    return dat
         
     def sub_mod(mod):
         print(mod)
     
    -subscriber = Subscriber("datasets", sub_init, sub_mod)
    -loop.run_until_complete(subscriber.connect(server, port))
     
    -time.sleep(5)
    +async def maintain_connection():
    +    subscriber = Subscriber("datasets", sub_init, sub_mod)
    +    await subscriber.connect(server, port)
    +    while True:
    +        await asyncio.sleep(0.1)
    +
    +loop.run_until_complete(maintain_connection())

    Key changes:

    1. Added shared dict() to maintain state
    2. Made sub_init return the state for sipyco to track
    3. Added maintain_connection() with continuous loop instead of time.sleep()
    4. Set the event loop explicitly