<div dir="ltr"><div>Hi All,</div>
<div>I'm trying to pick up Twisted and I'm having trouble with looping.</div>
<div> </div>
<div>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?</div>
<div> </div>
<div>Thanks,</div>
<div>Dave</div>
<div> </div>
<div>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!</div>
<div> </div>
<div> </div>
<div><font face="courier new,monospace">from twisted.internet import task, defer<br>from twisted.internet import reactor<br>import sys</font></div>
<div><font face="courier new,monospace">import time</font></div>
<div><font face="courier new,monospace"></font> </div>
<div><font face="courier new,monospace">def check_file_p(fp):<br> time.sleep(2)<br> #print fp<br> sys.stdout.write('starting check...\n')<br> fp.seek(fp.tell())<br> list = []<br> for line in fp.readlines():<br>
#sys.stdout.write(line)<br> list.append(line)<br> str_list = str(list)<br> d = defer.Deferred()<br> #print str_list<br> d.callback(str_list)<br> print d<br> return d</font></div>
<div><br><font face="courier new,monospace">def print_list(string):<br> print 'calling string list...'<br> sys.stdout.write('string: ' + string)</font></div>
<div><font face="courier new,monospace"></font> </div>
<div><font face="courier new,monospace">fn = r"C:\var\twisted.txt"<br>f = open(fn, 'r')</font></div>
<div><font face="courier new,monospace">#l = task.LoopingCall(runEverySecond)</font></div>
<div><font face="courier new,monospace">"""<br>didn't work<br>l = task.LoopingCall(check_file_p, f)<br>d = l.start(5.0, False) # call every x seconds<br>"""</font></div>
<div><font face="courier new,monospace">d = task.deferLater(reactor, 3.5, check_file_p, f)</font></div>
<div><br><font face="courier new,monospace">print d<br>d.addCallback(print_list)<br>print d</font></div>
<div><font face="courier new,monospace">print 'starting reactor'<br>#l.stop() #will stop the looping calls</font></div>
<div><font face="courier new,monospace">reactor.run()</font></div></div>