Nick Lunt wrote: > Hello folks, > > I have the following code taken from the Twisted examples - > > [code] > # filewatcher.py > from twisted.application import internet > > def watch(fp): > fp.seek(fp.tell()) > for line in fp.readlines(): > sys.stdout.write(line) > > import sys > from twisted.internet import reactor > s = internet.TimerService(1.0, watch, file(sys.argv[1])) > s.startService() > reactor.run() > s.stopService() > [/code] > > I find this piece of code amazing and I am keen to put it to use. > If I run './filewatcher.py myfile' it will print out any changes made to > 'myfile', very similar to 'tail -f' . > > Now if I want to have this program monitor several files at once I could > run './filewatcher.py file1 file2 filex' or './filewatcher.py file1 & > ./filewatcher file2 & etc' both with minor modifications to the code, > but I think that could cause performance problems relating to the OS. > So I'm thinking I will need to learn python threads (no bad thing) > instead, but Im hoping that someone could tell me if that seems the best > way to go ?
What performance problems you you anticipate? I don't know much about Twisted but my understanding is that tasks are run in a single thread when they are ready. In your case you are scheduling a simple task to run every second. I would think that you could schedule several such tasks and they would each run every second. If your task were time-consuming you might have to worry about doing something different but in this case I think it will be fine. Just try something like for name in sys.argv[1:]: s = internet.TimerService(1.0, watch, file(name)) s.startService() I suppose if you used threads there would be the possibility of a context switch while watch() is running, if one thread becomes blocked on I/O then another thread can run. I don't know how Twisted handles this - I think you have to wrap the file and stdio in Twisted object that handle the blocking. twisted.protocols.basic.FileSender and twisted.internet.stdio.StandardIO look like they may be starting points. Kent _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor