Created
December 8, 2011 20:45
-
-
Save jamesgecko/1448494 to your computer and use it in GitHub Desktop.
Feed list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Unhandled Error | |
Traceback (most recent call last): | |
File "sscce.py", line 32, in <module> | |
reactor.run() | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1162, in run | |
self.mainLoop() | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1171, in mainLoop | |
self.runUntilCurrent() | |
--- <exception caught here> --- | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 766, in runUntilCurrent | |
f(*a, **kw) | |
File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 360, in callback | |
assert not isinstance(result, Deferred) | |
exceptions.AssertionError: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Objective: get a list of feeds from a database and print them out. | |
from twisted.internet import reactor, task, threads | |
from twisted.enterprise.adbapi import ConnectionPool | |
from twisted.web.client import Agent | |
def get_feed_urls(db): | |
''' Returns a list of each feed's url and name''' | |
return db.runInteraction(select_url) | |
def select_url(txn): | |
''' Does db fetch work in thread for get_feed_urls ''' | |
txn.execute("SELECT url, source FROM feeds") | |
return txn.fetchall() | |
def update(db, agent): | |
''' Get a list of feeds ''' | |
d = threads.deferToThread(get_feed_urls, db) | |
d.addCallback(update_callback, agent) | |
def update_callback(feed_list, agent): | |
''' Add all new entries in every feed to the database. ''' | |
print str(feed_list) | |
# ... | |
if __name__ == '__main__': | |
db_url = 'test.sqlite' | |
db = ConnectionPool('sqlite3', db_url, check_same_thread = False) | |
agent = Agent(reactor) | |
loopingCall = task.LoopingCall(update, db, agent) | |
loopingCall.start(2, False) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment