I forgot how easy this is now:
import anvil.server
from contextlib import contextmanager
@contextmanager
def open_anvil_connection(uplink_key):
anvil.server.connect(uplink_key)
print("Connected to Anvil")
yield
anvil.server.disconnect()
print("Disconnected from Anvil")
Thats it.
Now you can use:
with open_anvil_connection(my_uplink_key):
stuff = anvil.server.call("some_remote_function")
results = blah_blah_do_crunchy_crunchy_on_stuff(stuff)
anvil.server.call('write_back_crunched_stuff', results )
…and then it closes automatically.