I’m trying to create a small IRC connection with Python, now I want it to be able to connect to multiple servers so I created a server class that will connect to the server, I wanted to save the servers onto a db as well so I used sqlalchemy to do that. This is the init on the server class
def __init__(self, server_name, server_address, server_port=6667):
threading.Thread.__init__(self)
self.server_name = server_name
self.server_address = server_address
self.server_port = server_port
Now the weird part is that it actually works if I call create a class “manually” like this
bla = IRCLib.connect("local","127.0.0.1")
this way it connects and there is no problem at all, however when I load the class through SQLAlchemy like this
bla = db.BaseService.session.query(IRCLib.server)
res = bla.all()
res[0].connect()
I get this error
raise RuntimeError("thread.__init__() not called")
Now the connect and run function on the class are this
def connect(self):
self.irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
self.irc.connect ( ( self.server_address, self.server_port ) )
self.irc_file = self.irc.makefile("rb")
self.start()
def run(self):
print "in run"
self.irc.send ( 'NICK Test\r\n' )
self.irc.send ( 'USER lsb lsb lsb :lsb\r\n' )
while True:
data = self.irc_file.readline()
print data
I’m really not sure what I can do to get it to work with SQLAlchemy, because like I said, it works if I do it manually.
Thanks
The
__init__()method isn’t called when SQLAlchemy loads an object from the database (docs: http://docs.sqlalchemy.org/en/rel_0_7/orm/mapper_config.html#constructors-and-object-initialization). To perform a task each time an object is loaded, use the@orm.reconstructorhook described in that section: