Something like this:<br><br>from twisted.protocols import basic<br>from twisted.internet import protocol, reactor<br>from twisted.enterprise import adbapi<br><br>class MyProtocol(basic.LineReceiver):<br> def __init__(self):<br>
pass<br><br> def lineReceived(self, line):<br> ### dbcon can be accessed via self.factory<br> print dir(self.factory.dbcon)<br><br>class MyFactory(protocol.ServerFactory):<br> protocol = MyProtocol<br>
def __init__(self):<br> self.dbcon = adbapi.ConnectionPool("pyPgSQL.PgSQL", database="data", user='pet', host='local', password='some')<br><br>if __name__ == "__main__":<br>
reactor.listenTCP(8080, MyFactory())<br> reactor.run()<br><br>I usually put persistent data on the factory so that protocol instances can access them such as {username:<protocol instance>} dictionary for chatroom server.<br>
<br><br><br><div class="gmail_quote">On Wed, Mar 25, 2009 at 12:22 AM, Pet <span dir="ltr"><<a href="mailto:petshmidt@googlemail.com">petshmidt@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Tue, Mar 24, 2009 at 5:04 PM, Rob Hoadley <<a href="mailto:hoadley@gmail.com">hoadley@gmail.com</a>> wrote:<br>
> You'd want to use a connection pool to manage the db interaction.<br>
> Your server is pretty unusable after a db connection failure. I've<br>
> used the connection pool before with a cp_min of 1 and a cp_max of 2.<br>
<br>
</div>Honestly speaking, I don't understand, what does it mean.<br>
I'm already using connection pool with default cp_min an max, don't I?<br>
Could you explain me, as for a newbie, please<br>
<div><div></div><div class="h5"><br>
><br>
> <a href="http://twistedmatrix.com/documents/8.2.0/api/twisted.enterprise.adbapi.ConnectionPool.html" target="_blank">http://twistedmatrix.com/documents/8.2.0/api/twisted.enterprise.adbapi.ConnectionPool.html</a><br>
><br>
><br>
> On Tue, Mar 24, 2009 at 6:45 AM, Alvin Delagon <<a href="mailto:adelagon@gmail.com">adelagon@gmail.com</a>> wrote:<br>
>> Put self.dbcon in the MyFactory class. MyProtocol instances can access it<br>
>> via self.factory.<br>
>><br>
>><br>
>> On Tue, Mar 24, 2009 at 6:10 PM, Pet <<a href="mailto:petshmidt@googlemail.com">petshmidt@googlemail.com</a>> wrote:<br>
>>><br>
>>> Hi,<br>
>>><br>
>>> I've wrote an daemon which does some queries to db and sends response<br>
>>> back to client. Do I need to make for every request from client (new<br>
>>> instance of MyProtocol) a new connection to DB? Or can I somehow<br>
>>> prepare connection, so I could save some time? Maybe make connection<br>
>>> in Factory and pass it to Protocol? But what happens if too much<br>
>>> clients are connected to server? What is the way to control it?<br>
>>><br>
>>> Regards, Pet<br>
>>><br>
>>><br>
>>> class MyProtocol(basic.LineReceiver):<br>
>>><br>
>>> def __init__(self):<br>
>>> print "new connection"<br>
>>> self.dbcon = adbapi.ConnectionPool("pyPgSQL.PgSQL",<br>
>>> database="data", user='pet', host='local', password='some')<br>
>>><br>
>>><br>
>>> class MyFactory(protocol.ServerFactory):<br>
>>> protocol = MyProtocol<br>
>>><br>
>>> class MyService(internet.TCPServer):<br>
>>> def __init__(self):<br>
>>> internet.TCPServer.__init__(self,PORT,MyFactory())<br>
>>><br>
>>> def main():<br>
>>> reactor.listenTCP(PORT, MyFactory())<br>
>>> reactor.run()<br>
>>><br>
>>><br>
>>> if __name__ == '__main__':<br>
>>> main()<br>
>>><br>
>>> _______________________________________________<br>
>>> Twisted-Python mailing list<br>
>>> <a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
>>> <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
>><br>
>><br>
>><br>
>> --<br>
>> <a href="http://www.alvinatorsplayground.blogspot.com/" target="_blank">http://www.alvinatorsplayground.blogspot.com/</a><br>
>><br>
>> _______________________________________________<br>
>> Twisted-Python mailing list<br>
>> <a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
>> <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
>><br>
>><br>
><br>
> _______________________________________________<br>
> Twisted-Python mailing list<br>
> <a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
> <a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
><br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.alvinatorsplayground.blogspot.com/">http://www.alvinatorsplayground.blogspot.com/</a><br>