Module import difference between server vs uplink; server module imported twice?

I would like to develop more complex code in PyCharm. To that end I check out the code in Pycharm and run the code as uplink code.
When exploring this way of development I stumbled on two problems, which may well be my limited understanding.

I have a server module ‘data_access’ that I want to import in another module ‘plots’.
It seems that in server code one needs an import like:

    from . import data_access as da

where in uplink code one needs:

    import data_access as da

(in PyCharm I have set the folder ‘server_code’ as a so-called sources root, which will put it on the PYTHONPATH).

Then I tried a try/except to make the code easier to use in either context:

try:
    from . import data_access as da
    print('online import')
except:
    import data_access as da
    print('uplink import')

This works, but then I noticed that when the app runs I see the line ‘online import’ twice. So it seems the code is imported twice.

If someone could shed some light on this or has better import solutions, that would be most welcome. TIA

Is it possible that you have “circular” imports? A imports B imports A sort of thing? It’s easy for that to happen by accident.

Don’t think so.

I checked today and only see 1 import statement for every page view.
When I posted Anvil acted strangely (extremely slow responses). Things seemed to have returned to normal today.

Still, I see an import for every server call?!

And my Q about the best way to handle server vs uplink imports remain.

TIA

The python script starts every time you call the server unless you keep it running. Here you can find more details.

Ah, thanks for the pointer.
It kinda sucks that one cannot have server global vars. For instance setting up (external) database connections can take time. Not having global vars rules out connection pools.

Keeping the server running does allow server globals.
You can’t rely on them being there at the next server call, but they usually are there.
I haven’t tried any connection pool because I only use Anvil tables, but I think it’s worth trying. Once in a while the pool will need to be reinstantiated, but when it persists it should help.

they usually are there

that doesn’t sound very deterministic :cold_sweat:
Maybe as a sort of caching it might be useful but otherwise I prefer my variables to stay around.

No, it is not. But that’s how large web sites work when one server alone is not enough to manage all the traffic. You just don’t know what server will be there listening to you.

I have a dedicated server, that is there is only one server listening to me and perhaps the interpreter is more likely to stay up listening to the next request. But I’m sure there is some housekeeping going on that will shut down processes once in a while.

Another tool you could use is the session (well, not for a connection pool!).

2 Likes

Maybe as a sort of caching it might be useful

That is, indeed, exactly its intended function! For example, you said earlier, “setting up (external) database connections can take time”. That’s a perfect use case for persistent servers – you’ll only take the setup cost very occasionally, as opposed to every time you want to talk to the DB.

1 Like

Yikes, “Keep server running” is only available on the Business plan?! I really can’t justify that plan for myself as an amateur solo dev.

@meredydd Can you confirm that a construct like

try:
    from . import data_access as da
except:
    import data_access as da

is the recommended way of handling the differences in imports between server code and uplink code? TIA

1 Like