1 #!/usr/bin/env python 2 3 """ 4 Example of concurrency when reading files. 5 6 See: http://wiki.python.org/moin/Concurrency/99Bottles 7 8 Based on the generator version. 9 """ 10 11 import pprocess 12 import time 13 import re 14 15 def follow(ch, fname): 16 f = file(fname) 17 f.seek(0,2) # go to the end 18 while True: 19 l = f.readline() 20 if not l: # no data 21 time.sleep(.1) 22 else: 23 ch.send(l) 24 25 def grep(ch, lines, pattern): 26 regex = re.compile(pattern) 27 for l in lines: 28 if regex.match(l): 29 ch.send(l) 30 31 def printer(lines, delay=5000): 32 while 1: 33 lines.store(delay) 34 if len(lines) > 0: 35 l = lines.next() 36 print l.strip() 37 else: 38 print "No input received for %d ms." % delay 39 40 def multigrep(ch, pattern): 41 queue = pprocess.Queue(continuous=1) 42 multifollow = queue.manage(follow) 43 multifollow('test') # '/var/log/system.log' 44 multifollow('test2') 45 multifollow('test3') 46 47 # Handle incoming lines using the specified pattern. 48 grep(ch, queue, pattern) 49 50 # Permit multiple simultaneous grep activities. 51 queue = pprocess.Queue(continuous=1) 52 multigrep = queue.manage(multigrep) 53 multigrep(".*pants.*") 54 multigrep(".*trousers.*") 55 multigrep(".*shorts.*") 56 57 p = printer(queue) 58 59 # vim: tabstop=4 expandtab shiftwidth=4