Newbie question

Golawala, Moiz M (GE Infrastructure) Moiz.Golawala at ge.com
Fri Jul 23 15:25:55 CEST 2004


Hi Harry,

Try this piece of code:

from threading import Thread

class fooThread(Thread):
	def __init__(self):
		Thread.__init__(self)
		self.stopFlag = True
	
	def run(self):
		file="...."
		mtime=os.path.getmtime(file)
		while self.stopFlag:
			if os.path.getmtime(file)!=mtime:
		      	print "%s changed" % file
				mtime=os.path.getmtime(file)
				time.sleep(1)
		

	def stop(self):
		self.stopFlag = False

This is what you will write in you main class. You will start up the Thread and control it here.

class myMainClass:
	def __init__(self):
		self.foo = fooThread()
		
	def startThread(self):
		"""
			This is how you start the thread
		"""
		self.foo.start()

	def stopThread(self):
		"""
			This is how you stop the thread
		"""

		self.foo.stop()

for more information on threads read this. (http://docs.python.org/lib/module-threading.html)

Cheers
Moiz


-----Original Message-----
From: python-list-bounces+moiz.golawala=ge.com at python.org
[mailto:python-list-bounces+moiz.golawala=ge.com at python.org]On Behalf Of
Harry Knitter
Sent: Friday, July 23, 2004 7:41 AM
To: python-list at python.org
Subject: Re: Newbie question


Thomas Guettler wrote:

> Am Fri, 23 Jul 2004 13:03:47 +0200 schrieb Harry Knitter:
> 
>> Hello,
>> 
>> I am new with Python and would like to know how to achieve to make a
>> Python
>> program continously  listening if a certain data file is changed from
>> another program.
>> Thanks
> 
> untested:
> 
> import os
> import time
> 
> file="...."
> mtime=os.path.getmtime(file)
> while 1:
>     if os.path.getmtime(file)!=mtime:
>         print "%s changed" % file
>         mtime=os.path.getmtime(file)
>     time.sleep(1)
> 
Thanks, however, how do I have to invoke this procedure avoiding the program
remaining in this endles loop.What I want to achieve is, that the program
does something, when the file is changed an then listening again. Meanwhile
the user shoud be able to use the program as usual. i.e. I need this
procedure as a background process triggering a special action.

Harry
-- 
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list