In web2py, and in general in code that runs multithreaded you should not define variables in modules at top level and then import them because they are cached and then shared by multiple threads. That is not thread safe and will cause all kind of problems. Modules should define functions and classes, not objects. You should only import those functions and classes.
On Wednesday, 20 September 2017 02:25:14 UTC-5, Brendan Barnwell wrote: > > It seems that the "db" object given by the DAL wraps up not only the > database definition, but also a "live" connection to the database. This > means that if the db object is stashed somewhere, it can go "stale" and > attempts to use it later will lead to strange errors. Is that so? > > What I'm trying to do is to pre-build a query constraint like > "db.MyTable.myfield < 2", within a module. Later in controller code I may > retrieve these stored constraints and string them together using &. But > right now it seems I have to build the constraints anew within a function, > because if I do something like "stored_query = db.MyTable.myfield < 2" at > the top level of module code, the db object goes stale. Later I get errors > such as "name conflict in table list", apparently because it doesn't > realize the stored "db.MyTable" is the same as "real_db.MyTable" (where > "real_db" is the new live db object for the current request). > > It is not a huge deal to regenerate the constraints by calling a function, > but it would be nice if they could just be stored as static objects. Is > there any way to use the DAL's nice query-building syntax without a live DB > connection, and then later used one or more stored query constraints when > querying a live db connection? Something like: > > # in somemodule.py > db = ? # get non-live DB object to build query > query1 = db.MyTable.myfield < 2 > query2 = db.MyTable.otherfield == "blah" > > # in a controller > import somemodule > > def controller_function(): > # now db has a live connection > db(somemodule.query1 & somemodule.query2).select() > > Is such a thing possible? > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.