[DONE] Disconnect uplink

Probably a silly question, but once I’m connected to my app via uplink, how can I reconnect to a new app with uplink?

Just re-running anvil.server.connect(my_new_app_key) doesn’t disconnect the first connection, and so nothing changes.

1 Like

There’s not currently a way to disconnect a running Uplink script using a Python function call - the way to disconnect a script is to kill the process! Moving to Feature Requests.

Is it possible for you to restart your script? You can pass the key in as an argument on the commandline, just write your connect call as:

import sys

anvil.server.connect(sys.argv[1])
1 Like

Hi Shaun,

Thanks for the advice.

Currently I got around this by simply running two separate scripts and saving what I need to disk.

1 Like

+1 on this Feature.

FWIW, I am using anvil uplink code inside a Jupyter Notebook … and an I am tweaking some of the return data, so it fairly annoying to kill Jupyter process in order to re-connect to uplink.

Having a programatic way to refresh connection would be useful.

+1 for this feature. We have some related apps and doing maintenance across both uplinks is dreadful.

1 Like

Hey did this ever happen?

I want to use uplink on a device whos primary purpose is to utilize a network connection, then travel to parts of the warehouse where wifi is impossible to stay connected (under metal mezzanines), then come back.

Without the ability to disconnect the uplink, I think the device CLI will be constantly interrupted by uplink disconnected errors, and the user uses the CLI to interact with the program.

You can call

anvil.server.disconnect()

And then connect with a different key.

2 Likes

Thanks, @owen.campbell ! I’ve added it to Sneaky Anvil Updates, with pointers to documentation:

Would you happen to know whether anvil.server.disconnect() cancels (terminates) an active anvil.server.wait_forever()? It would be logical for it to do that.

Oooh, nice.

Now its time to build my own context manager to open a connection, use it, then tear it down correctly.
:grinning: :man_technologist:

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.

4 Likes

In the words of Homer Simpson …“Yoink”

#whyreinventthewheel

Thank you @ianbuywise

2 Likes