[Tutor] Should I be thinking of threads for this ?
Nick Lunt
nick at javacat.f2s.com
Thu Aug 25 21:28:45 CEST 2005
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 ?
I will be getting to grips with python threads anyway but I'd appreciate
any input on this.
Many thanks,
Nick .
More information about the Tutor
mailing list