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 44 # Launch concurrent following activities. 45 multifollow('/tmp/test') # '/var/log/system.log' 46 multifollow('/tmp/test2') 47 multifollow('/tmp/test3') 48 49 # Handle incoming lines using the specified pattern. 50 grep(ch, queue, pattern) 51 52 # Permit multiple simultaneous grep activities. 53 queue = pprocess.Queue(continuous=1) 54 multigrep = queue.manage(multigrep) 55 56 # Launch concurrent grep activities. 57 multigrep(".*pants.*") 58 multigrep(".*trousers.*") 59 multigrep(".*shorts.*") 60 61 # Print incoming lines. 62 p = printer(queue) 63 64 # vim: tabstop=4 expandtab shiftwidth=4