[Twisted-Python] Looping

David Wilson david.craig.wilson at gmail.com
Fri Sep 19 22:28:27 MDT 2008


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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080919/e183eeea/attachment.htm 


More information about the Twisted-Python mailing list