loopquery.py
changeset 51 bdc44f96cb0b
child 62 af637235ca81
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loopquery.py	Fri Oct 05 14:31:25 2012 +0200
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3.2
+"""
+loopquery
+
+Execute some query in loop, waiting for some time interval before next run.
+
+"""
+
+from pgtoolkit import toolbase
+
+import time
+
+
+class LoopQueryTool(toolbase.SimpleTool):
+    def __init__(self):
+        toolbase.SimpleTool.__init__(self, name='loopquery', desc='Run query in loop.')
+        self.parser.add_argument('-q', dest='query', type=str, help='Query to run.')
+        self.parser.add_argument('-i', dest='interval', type=float, help='Run interval in seconds.')
+        self.init()
+
+    def main(self):
+        while True:
+            self.log.info('Executing: %s', self.args.query)
+            with self.pgm.cursor('target') as curs:
+                curs.execute(self.args.query)
+            self.log.info('Done.')
+            time.sleep(self.args.interval)
+
+
+tool = LoopQueryTool()
+tool.main()
+