Cannot access db in a thread

29 views
Skip to first unread message

Harshad

unread,
Dec 2, 2016, 6:34:49 PM12/2/16
to web2py-users
I have a separate process that I run along with my web application by creating a subprocess like so:

           subprocess.Popen("python web2py.py -v -e -a admin -i 0.0.0.0 -p 8080 -c <cert_file> -k <key_file>".split())
subprocess.Popen("python web2py.py -S <app_name> -M -R <path_to_process>".split())

In that process, I start a thread to do some tasks. I have assigned the callbacks from that thread to the callbacks in that process:

Class Process:

    def __init__(self):
        self.thread = MyThread() #MyThread is a threading.Thread object
        self.thread.callback = self.callback
        self.thread.start()

    def self.callback(self, event):
        print db(auth_user.id=1).first_name


In the self.callback, when I try to read from the database using the db object I get the following error:

AttributeError: 'thread._local' object has no attribute '_pydal_connection_55197392_9964'

I have tried making the db object a member of the Process class and then trying to access it in the callback:

Class Process:

    def __init__(self):
        self.db = db
        self.thread = Threading.thread()
        self.thread.callback = self.callback
        self.thread.start()

    def self.callback(self, event):
        print self.db(auth_user.id=1).first_name

I still get the same error.

How can I access the database from inside a thread created inside a process running in a shell?

Reply all
Reply to author
Forward
0 new messages