@web.background gotcha

Bug #133079 reported by Anand Chitipothu on 2007-08-17
Affects Status Importance Assigned to Milestone
Anand Chitipothu

Bug Description

Here is the problem.


And here is the fix.

def background(func):
    """A function decorator to run a long-running function as a
background thread.
+ GOTCHA: Until the foreground task ends, any db access by
background task
+ will necessarily get old data from before the foreground task
+ because psycopg2 begins a transaction in the foreground task
until it quits """
    def internal(*a, **kw):
        web.data() # cache it

        tmpctx = web._context[threading.currentThread()]
        web._context[threading.currentThread()] =

        def newfunc():
            web._context[threading.currentThread()] = tmpctx
+ # Create new db cursor if there is one else background thread
+ # overwrites foreground cursor causing rubbish data into dbase
+ if web.config.get('db_parameters'):
+ webdb.connect(**web.config.db_parameters)
            func(*a, **kw)

Changed in webpy:
assignee: nobody → anandology
importance: Undecided → High
status: New → In Progress
Changed in webpy:
milestone: 0.22 → 0.23
Changed in webpy:
status: In Progress → Fix Committed
Changed in webpy:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers