How many times does this client code call the server?

FWIW here’s some code we put in our startup module when we really want to know what server calls are occuring. It’s monkey patching, super gross, and should NOT be used in production, but can be really helpful for trouble-shooting performance issues related to server calls (that you may or may not expect).

from datetime import datetime
import anvil.server 

original_callable = anvil.server.call

def new_callable(fn, *args, **kwargs):
    start = datetime.now()
    print(f"Calling {fn} with: args: {args}, kwargs: {kwargs}")
    value = original_callable(fn, *args, **kwargs)
    str_value = str(value)
    if len(str_value) > 100:
        str_value = f"{str_value[0:100]}... ({len(str_value)} total chars)"
    print(f"    --> {fn} took {datetime.now() - start}: returned: {str_value}")
    return value

anvil.server.call = new_callable

# If at any point you want to dynamically "turn off" this level 
# of detail, you can simply reset anvil.server.call:

anvil.server.call = original_callable
1 Like