[Twisted-Python] db connections

Pet petshmidt at googlemail.com
Wed Mar 25 03:22:40 MDT 2009


On Tue, Mar 24, 2009 at 6:10 PM, Alvin Delagon <adelagon at gmail.com> wrote:
> Something like this:
>
> from twisted.protocols import basic
> from twisted.internet import protocol, reactor
> from twisted.enterprise import adbapi
>
> class MyProtocol(basic.LineReceiver):
>     def __init__(self):
>         pass
>
>     def lineReceived(self, line):
>         ### dbcon can be accessed via self.factory
>         print dir(self.factory.dbcon)
>
> class MyFactory(protocol.ServerFactory):
>     protocol = MyProtocol
>     def __init__(self):
>         self.dbcon = adbapi.ConnectionPool("pyPgSQL.PgSQL", database="data",
> user='pet', host='local', password='some')
>
> if __name__ == "__main__":
>     reactor.listenTCP(8080, MyFactory())
>     reactor.run()
>
> I usually put persistent data on the factory so that protocol instances can
> access them such as {username:<protocol instance>} dictionary for chatroom
> server.

Hi,

thanks for example!
In that way, I'm getting error:

exceptions.AttributeError: MyProtocol instance has no attribute 'factory'

How can MyProtocol access self.factory.dbcon?


Pet




>
>
>
> On Wed, Mar 25, 2009 at 12:22 AM, Pet <petshmidt at googlemail.com> wrote:
>>
>> On Tue, Mar 24, 2009 at 5:04 PM, Rob Hoadley <hoadley at gmail.com> wrote:
>> > You'd want to use a connection pool to manage the db interaction.
>> > Your server is pretty unusable after a db connection failure.  I've
>> > used the connection pool before with a cp_min of 1 and a cp_max of 2.
>>
>> Honestly speaking, I don't understand, what does it mean.
>> I'm already using connection pool with default cp_min an max, don't I?
>> Could you explain me, as for a newbie, please
>>
>> >
>> >
>> > http://twistedmatrix.com/documents/8.2.0/api/twisted.enterprise.adbapi.ConnectionPool.html
>> >
>> >
>> > On Tue, Mar 24, 2009 at 6:45 AM, Alvin Delagon <adelagon at gmail.com>
>> > wrote:
>> >> Put self.dbcon in the MyFactory class. MyProtocol instances can access
>> >> it
>> >> via self.factory.
>> >>
>> >>
>> >> On Tue, Mar 24, 2009 at 6:10 PM, Pet <petshmidt at googlemail.com> wrote:
>> >>>
>> >>> Hi,
>> >>>
>> >>> I've wrote an daemon which does some queries to db and sends response
>> >>> back to client. Do I need to make for every request from client (new
>> >>> instance of  MyProtocol) a new connection to DB? Or can I somehow
>> >>> prepare connection, so I could save some time? Maybe make connection
>> >>> in Factory and pass it to Protocol? But what happens if too much
>> >>> clients are connected to server? What is the way to control it?
>> >>>
>> >>> Regards, Pet
>> >>>
>> >>>
>> >>> class MyProtocol(basic.LineReceiver):
>> >>>
>> >>>    def __init__(self):
>> >>>        print "new connection"
>> >>>        self.dbcon = adbapi.ConnectionPool("pyPgSQL.PgSQL",
>> >>> database="data", user='pet', host='local', password='some')
>> >>>
>> >>>
>> >>> class MyFactory(protocol.ServerFactory):
>> >>>    protocol = MyProtocol
>> >>>
>> >>> class MyService(internet.TCPServer):
>> >>>    def __init__(self):
>> >>>        internet.TCPServer.__init__(self,PORT,MyFactory())
>> >>>
>> >>> def main():
>> >>>    reactor.listenTCP(PORT, MyFactory())
>> >>>    reactor.run()
>> >>>
>> >>>
>> >>> if __name__ == '__main__':
>> >>>    main()
>> >>>
>> >>> _______________________________________________
>> >>> Twisted-Python mailing list
>> >>> Twisted-Python at twistedmatrix.com
>> >>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>> >>
>> >>
>> >>
>> >> --
>> >> http://www.alvinatorsplayground.blogspot.com/
>> >>
>> >> _______________________________________________
>> >> Twisted-Python mailing list
>> >> Twisted-Python at twistedmatrix.com
>> >> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>> >>
>> >>
>> >
>> > _______________________________________________
>> > Twisted-Python mailing list
>> > Twisted-Python at twistedmatrix.com
>> > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>> >
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
>
> --
> http://www.alvinatorsplayground.blogspot.com/
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>




More information about the Twisted-Python mailing list