couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Trivial Update of "Regenerating_views_on_update" by JamesArthur
Date Sun, 07 Feb 2010 23:02:29 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "Regenerating_views_on_update" page has been changed by JamesArthur.
The comment on this change is: Ah yes, the actual version.
http://wiki.apache.org/couchdb/Regenerating_views_on_update?action=diff&rev1=6&rev2=7

--------------------------------------------------

  import logging
  logging.basicConfig(level=logging.INFO)
  
- import fcntl
  import os
  import re
+ import signal
  import sys
  import time
  import urllib2
  
  from threading import Thread
  
+ flags = {
- is_running = True
+     'is_running': True
+ }
+ 
  changed_docs = {}
  
  class ViewUpdater(object):
@@ -166, +169 @@

              ]
          }
      }
-         
+     
      def start(self):
          Thread(target=self._run).start()
      
      
      def _run(self):
          """Loop, checking for enough ``changed_docs`` to trigger a
-           request to CouchDB to re-index.
+           request to couchdb to re-index.
          """
          
-         global is_running
-         while is_running:
+         while flags['is_running']:
              try:
                  for db_name, number_of_docs in changed_docs.items():
                      if number_of_docs >= self.MIN_NUM_OF_CHANGED_DOCS:
@@ -195, +197 @@

                                      urllib2.urlopen(url)
                  time.sleep(self.PAUSE)
              except Exception:
-                 is_running = False
+                 flags['is_running'] = False
                  raise
              
          
@@ -210, +212 @@

      
      DB_NAME_EXPRESSION = re.compile(r'\"db\":\"(\w+)\"')
      
-     def __init__(self):
-         """Make stdin a non-blocking file
-         """
-         
-         fd = sys.stdin.fileno()
-         fl = fcntl.fcntl(fd, fcntl.F_GETFL)
-         fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
-         
-     
-     
-     def run(self):
+     def _run(self):
          """Consume update notifications from stdin.
          """
          
-         global is_running
-         while is_running:
+         while flags['is_running']:
              try:
                  data = sys.stdin.readline()
              except:
                  continue
              else:
                  if not data: # exit
-                     is_running = False
+                     flags['is_running'] = False
                      break
                  result = self.DB_NAME_EXPRESSION.search(data)
                  if result:
@@ -249, +240 @@

          
      
      
+     def start(self):
+         t = Thread(target=self._run)
+         t.start()
+         return t
+         
+     
+     
  
  
  def main():
@@ -256, +254 @@

      consumer = NotificationConsumer()
      updater = ViewUpdater()
      updater.start()
+     t = consumer.start()
      try:
-         consumer.run()
+         while flags['is_running']:
+             t.join(10)
      except KeyboardInterrupt, err:
-         is_running = False
+         flags['is_running'] = False
+         
      
  
  

Mime
View raw message