[Tutor] Should I be thinking of threads for this ?
Andreas Kostyrka
andreas at kostyrka.org
Fri Aug 26 19:39:40 CEST 2005
Am Donnerstag, den 25.08.2005, 20:28 +0100 schrieb Nick Lunt:
> 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 = [internet.TimerService(1.0, watch, file(x) for x in sys.argv[1:]]
for x in s: x.startService()
> 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,
Well, compared to threads, executing a stat and/or read (I guess it goes
the read way, which is interesting, usually one watches files by doing
stat) is not relevant.
> 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)
How would adding Threads to the mix solve any performance problems *g*
Andreas
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20050826/d959ebd8/attachment.pgp
More information about the Tutor
mailing list