This file contains hidden or 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
def run_query(query, args): | |
return Query().add(query, args).run() | |
def run_queries(*queries): | |
q = Query() | |
for query, args in queries: q.add(query, args) | |
return q.run() | |
result = run_query("SELECT * FROM TRANSACTIONS WHERE ID=?", [id]) |
This file contains hidden or 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
q = Query() | |
q.add("UPDATE TRANSACTIONS SET STATUS=? WHERE ID=?", [new_status, id]) | |
q.run() | |
# To get a result, use get() | |
result = q.get() | |
# Or chain run() and get() | |
result = q.run().get() |
This file contains hidden or 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
def start(): | |
t = Thread(target=dispatcher) | |
t.start() | |
def shutdown(): | |
_dispatch_queue.put(None) |
This file contains hidden or 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
class Query(Task): | |
@staticmethod | |
def executor(qname): | |
cx = sqlite3.connect(get_dbfile()) | |
cx.row_factory = sqlite3.Row | |
queue = get_queue(qname) | |
while True: | |
task = queue.get() | |
for query, args in task.queries: | |
result = cx.execute(query, args) |
This file contains hidden or 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
_dispatch_queue = Queue() | |
_queues = {} | |
def get_queue(qname): | |
return _queues[qname] | |
def dispatcher(): | |
while True: | |
task = _dispatch_queue.get() | |
if not task: break # "shutdown" happens by putting None on the queue, so it will break here |
NewerOlder