How to end infinite while loop from uplink

This thread might be useful

The solution involved running the raspberry pi logic in a separate thread so that anvil server calls would be able to interrupt the execution with global flags.

I think the same type of solution will work here.

Edit: I missed that you already mentioned you were using threading.