Skip to content Skip to sidebar Skip to footer

Twisted Loopingcall With Blocking Function

I have an application which needs to poll a database for possible configuration changes. The application is a simple xmlrpc server using Twisted. I have experimented using Twiste

Solution 1:

You should use twisted.enterprise.adbapi module. It will give you nonblocking api for all DBAPI 2.0 compatible clients by running them in thread pool and returning standard Deferred to you:

from twisted.enterprise import adbapi


dbpool = adbapi.ConnectionPool('psycopg2', 'mydb', 'andrew', 'password') # replace psycopg2 with your db client name.defgetAge(user):
    return dbpool.runQuery('SELECT age FROM users WHERE name = ?', user)

defprintResult(l):
    if l:
        print l[0][0], "years old"else:
        print"No such user"

getAge("joe").addCallback(printResult)

For more information and examples please visit official documentation: https://twistedmatrix.com/documents/14.0.0/core/howto/rdbms.html

Post a Comment for "Twisted Loopingcall With Blocking Function"