Is there a way to import one server module into a different server module? I want to make a ‘utilities’ module that can be imported in multiple places. If this was mentioned in the server module documentation I missed it.
If I try and import either a plain module or server module into another server module, I get very strange behavior: it doesn’t error right away, it just churns and then spits out:
anvil.server.TimeoutError: Server code took too long
I would very much appreciate the ability to split up my server code so I don’t have just one monolithic module (and having an error other than timeout for the above situation would also have saved me some time).
OK, so it turns out that the TimeoutError was not caused by the import, it was caused by this line in a utility function within the imported module:
row = app_tables.keys.get(key_id=0)
This is actually more perplexing, since I would think a line like this would either immediately return or fail, not hang. Does this look familiar to anyone?
The ability to identify the actual line that hung on the server side (not just on the client side) would be immensely helpful as well.
How many rows does the table have?
I’ve never known it take long for a search (I’ve had 100k+ before).
Hi Joe,
That line of code is entirely legitimate, and shouldn’t hang. This is, at least, unexpected behaviour from Anvil, so I’d like to check it out. Are you able to share a copy of your app with me so I can take a look? (If you open the Publish dialog, you can find a link for “allow someone else to make a copy of this app” - you can post it here in the thread or private-message it to me)
thank you for your replies – it was not an issue with the ‘get’ function specifically, and the table in question only had one row.
After much trial and error, I’ve figured out that this is caused by calling the data tables ‘get’ function inside a function that was invoked at the module level (in an attempt to get some values that are needed by all the functions in the module). My best guess is that this is some kind of race condition with the data tables service initialization.
Is this is a built-in limitation of the framework? For now I’ll just call ‘get’ from every function in my module (or create a new decorator or something).
@meredydd, I’ve emailed you a small app that replicates this behavior.
Brilliant, thanks. Yes, that is definitely a bug – you should be able to use data tables in module-level initialisation. (Do you know whether it occurs only on in the “Restricted Python 2” runtime, or also in the “full” runtime [2.7 or 3.6]? I’ll check it all out tomorrow, but if it’s only in one runtime, you could carry on developing for now by switching…)
(Moved to Bug Reports section)
@meredydd, I have some python 3.6 specific code, so I’ve only been using the 3.6 runtime.