Thanks, that's good to know. I'll look into the scheduler. On Saturday, January 10, 2015 at 7:54:39 PM UTC-6, Massimo Di Pierro wrote: > > You should not create threads from insider a web applications. Not in > web2py. Not in any other web framework. > > This is because threads are managed by the web server which spawns a new > one when an http request arrives and kills them they they are completed or > timeout. This logic is outside of your control and will conflict with your > threads. depending on the web server you use, it may actually use processes > instead of threads and it may decide a kill a process with all its threads. > > This is why web2py has a scheduler. > > On Saturday, 10 January 2015 10:32:41 UTC-6, Michael Sounhein wrote: >> >> Currently I am new to programming and I thought Python and web2py would >> be a good way to learn. But, I am in it over my head when it comes to this >> problem. I'm getting an error when trying to access a db connection inside >> a Thread which is contained inside a module. The most pertinent part of the >> error is this. "if not self.connection: raise ValueError(a[0])" Here are >> the related pieces of code which are causing the problem. I've tried >> setting the value of db using a method inside coffee_server_threadR6. I >> have tried every way I can think of to get db into the module.(including >> current) and unfortunately once the tread is initialized I can't retrieve >> or set any data from my database. The only thing that does work is seeing >> which tables, and columns exist. So, it's as though once the variable db >> gets placed into the thread it no longer has a connection to the database >> at all. Any thoughts on this are appreciated. >> >> Models - db.py >> >> db = DAL('sqlite://storage.sqlite',pool_size=2,check_reserved=['all'], >> do_connect=True) >> >> Controllers - default >> >> @auth.requires_login() >> def startserv(): >> Server.ThreadedTCPRequestHandler.db = db >> reactor = Server.Reactor() >> reactor.daemon = True >> reactor.start() >> return redirect(URL('index')) >> >> Modules - coffee_server_threadR6.py >> >> #!/usr/bin/env python >> # -*- coding: utf-8 -*- >> from gluon import * >> from gluon import current >> import threading >> import time >> import SocketServer >> import struct >> import binascii >> from subprocess import Popen >> import databaseTest as Db_Conn >> >> >> SEND_WAKEUP = chr(1) >> AM_AWAKE = chr(0) >> >> class ThreadedTCPRequestHandler(SocketServer.StreamRequestHandler): >> >> cur_thread = threading.current_thread() >> db = None >> >> def wakeup_computer(self,mac): >> Popen(['echo', str(mac)]) >> time.sleep(3) >> Popen(['echo', str(mac)]) >> >> def handle(self): >> recv_data = self.request.recv(1024) >> packed_data = binascii.unhexlify(recv_data) >> s = struct.Struct('c 17s') >> self.unpacked_data = s.unpack(packed_data) >> self.macAddr = str(self.unpacked_data[1]) >> self.command = self.unpacked_data[0] >> response = "{}: {}".format(self.cur_thread.name, self.macAddr) >> self.request.sendall(response) >> self.updateDB(self.macAddr) >> >> def updateDB(self,mac): >> print self.db.computerstats.fields >> print self.db.computerstats.mac_addr.type >> print type(mac) >> comp = self.db(self.db.computerstats.id=="1").select().first() >> print comp >> self.db(self.db.computerstats.mac_addr==mac).update(status=1) >> self.db.commit() >> >> #self.db(self.db.computerstats.mac_addr=='AA:BB:CC:DD:EE:FF').update(status=True) >> >> class ThreadedTCPServer(SocketServer.ThreadingTCPServer): >> pass >> >> >> class CountThread(threading.Thread): >> def run(self): >> global TOTAL >> for i in range(100): >> TOTAL = TOTAL + 1 >> print('%s\n' % (TOTAL)) >> >> class Reactor(threading.Thread): >> >> def run(self): >> self.HOST, self.PORT = '', 8585 >> SocketServer.TCPServer.allow_reuse_address = True >> self.server = ThreadedTCPServer((self.HOST, self.PORT), >> ThreadedTCPRequestHandler) >> print "Server is set up" >> self.server.serve_forever() >> >> coffee_client.py - Executed from another computer. Currently testing from >> local machine. >> >> from socket import * >> import struct >> import binascii >> >> >> SEND_WAKEUP = chr(1) >> AM_AWAKE = chr(0) >> >> def send(data): >> sock = socket(AF_INET,SOCK_STREAM) >> sock.connect(('127.0.0.1',8585)) >> try: >> sock.send(data) >> response = sock.recv(1024) >> print "Received: {}".format(response) >> finally: >> sock.close() >> >> def wakeup(mac): >> values = (SEND_WAKEUP, mac) >> s = struct.Struct('c 17s') >> packed_data = s.pack(*values) >> hex_packed_data = binascii.hexlify(packed_data) >> send(hex_packed_data) >> >> def am_awake(mac): >> values = (AM_AWAKE, mac) >> s = struct.Struct('c 17s') >> packed_data = s.pack(*values) >> hex_packed_data = binascii.hexlify(packed_data) >> send(hex_packed_data) >> >> >> am_awake('AA:BB:CC:DD:EE:FF') >> >> >> >>
-- 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.