[Twisted-Python] Looping

Hi All, I'm trying to pick up Twisted and I'm having trouble with looping. I expected that a Deferred returned from a looping call would be available after each call. But it's only available after the loop is stopped. Am I looking for a different class? Thanks, Dave p.s. Ultimately I'd like to watch a file and post any new lines up to an HTTP server. If there is a direct way to get there, please let me know. Thanks! from twisted.internet import task, defer from twisted.internet import reactor import sys import time def check_file_p(fp): time.sleep(2) #print fp sys.stdout.write('starting check...\n') fp.seek(fp.tell()) list = [] for line in fp.readlines(): #sys.stdout.write(line) list.append(line) str_list = str(list) d = defer.Deferred() #print str_list d.callback(str_list) print d return d def print_list(string): print 'calling string list...' sys.stdout.write('string: ' + string) fn = r"C:\var\twisted.txt" f = open(fn, 'r') #l = task.LoopingCall(runEverySecond) """ didn't work l = task.LoopingCall(check_file_p, f) d = l.start(5.0, False) # call every x seconds """ d = task.deferLater(reactor, 3.5, check_file_p, f) print d d.addCallback(print_list) print d print 'starting reactor' #l.stop() #will stop the looping calls reactor.run()

David Wilson wrote:
It's not, and if you think about it, it can't - a loopingcall is setup once and run indefinitely, who would you be "returning" the deferreds to, and how? Just call your handler from within your loopingcall i.e. def check_file(fp, cb): # blah blah for line in lines: cb(line)
The code you show isn't a very robust way of following a file. My advice would be to run "tail -F -q name" in a sub-process and accumulate the output. You need to be aware when watching an external file that when you read, the whole line might not have been written, so you need to do something like this: buffer = '' def check(file, handler): buffer += file.read() if not '\n' in buffer: return lines = buffer.split('\n') # the final "line" in the buffer doesn't have a terminating # \n, make it be the buffer and drop it from out list buffer = lines.pop() for line in lines: handler(line)

David Wilson wrote:
It's not, and if you think about it, it can't - a loopingcall is setup once and run indefinitely, who would you be "returning" the deferreds to, and how? Just call your handler from within your loopingcall i.e. def check_file(fp, cb): # blah blah for line in lines: cb(line)
The code you show isn't a very robust way of following a file. My advice would be to run "tail -F -q name" in a sub-process and accumulate the output. You need to be aware when watching an external file that when you read, the whole line might not have been written, so you need to do something like this: buffer = '' def check(file, handler): buffer += file.read() if not '\n' in buffer: return lines = buffer.split('\n') # the final "line" in the buffer doesn't have a terminating # \n, make it be the buffer and drop it from out list buffer = lines.pop() for line in lines: handler(line)
participants (2)
-
David Wilson
-
Phil Mayers