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