473,796 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wait until change

Tom
Hi,

I have the following problem. I analyse data from a file. When I am done
analysing all the data that was in that file, I want to let my program
wait until this specific file has changed (which indicates that new data
has been added to the file from a third party program that I don't
control). If that file has changed, I want my program to continue.

What would be the appropriate command to check if the file changed? And
how can I implement "wait until" into my program? I googled a lot to
find any hints, but couldn't find anything helpful. But I found
something about a python that was stabbed in NY in 2000. :-) Thought
that was funny. :-)

I would deeply appreciate, if you could maybe include some little
fragments of code to show me how to use your suggestions. I am
relatively new to python and I often have problems with Pythons Library
Reference, because it doesn't show examples and is very technically.
That makes it difficult for me to really understand that. It also would
be great, if you could tell me which modules I have to include. I hope
all that is not asked too much. I assume that both problems can be
solved with a few easy lines of code. I just don't know them. :-)

Thanks a lot for your help and suggestions.
Regards Tom

Jul 18 '05 #1
19 9451
Tom wrote:

I have the following problem. I analyse data from a file. When I am done
analysing all the data that was in that file, I want to let my program
wait until this specific file has changed (which indicates that new data
has been added to the file from a third party program that I don't
control). If that file has changed, I want my program to continue.


Would it be sufficient to have your program periodically wake up and
check the timestamp on the file? To do that, a combination of time.sleep()
and os.stat() would be sufficient, and quite cross-platform.

-Peter
Jul 18 '05 #2
On Friday 17 October 2003 12:35 pm, Tom wrote:
Hi,

I have the following problem. I analyse data from a file. When I am done
analysing all the data that was in that file, I want to let my program
wait until this specific file has changed (which indicates that new data
has been added to the file from a third party program that I don't
control). If that file has changed, I want my program to continue.

What would be the appropriate command to check if the file changed? And
how can I implement "wait until" into my program? I googled a lot to
find any hints, but couldn't find anything helpful. But I found
something about a python that was stabbed in NY in 2000. :-) Thought
that was funny. :-)
I would use os.stat() which returns all sorts of info about the file including
modification time.

More info at: http://web.pydoc.org/1.5.2/os.html

time=os.stat(fi le)[8]

# analyze data

while 1:
if os.stat(file)[8]>time:
#analyze data again

I would deeply appreciate, if you could maybe include some little
fragments of code to show me how to use your suggestions. I am
relatively new to python and I often have problems with Pythons Library
Reference, because it doesn't show examples and is very technically.
That makes it difficult for me to really understand that. It also would
be great, if you could tell me which modules I have to include. I hope
all that is not asked too much. I assume that both problems can be
solved with a few easy lines of code. I just don't know them. :-)

Thanks a lot for your help and suggestions.
Regards Tom

I'm sure there are a number of ways to do this.

Good Luck,
LGL
Jul 18 '05 #3
Tom
Lane LiaBraaten wrote:
I would use os.stat() which returns all sorts of info about the file including
modification time.

More info at: http://web.pydoc.org/1.5.2/os.html

time=os.stat(f ile)[8]

# analyze data

while 1:
if os.stat(file)[8]>time:
#analyze data again

Hi Lane,

thanks for your answer it works great and gives me a lot of new ideas
that I can work with.
That "while 1" command looks interessting. I never used while that way.
The Library Reference doesn't explain it. But am I right if I assume
that this is a loop that continues forever? Despite using break of
course. I ask, because your suggestion works, but unfortunately uses
100% of my CPU power. :-( Well, it works, but is there maybe another way
to do the same without using so much of my resources? :-) That would be
great.

Thank's again.
Tom

Jul 18 '05 #4
Tom wrote:

thanks for your answer it works great and gives me a lot of new ideas
that I can work with.
That "while 1" command looks interessting. I never used while that way.
The Library Reference doesn't explain it. But am I right if I assume
that this is a loop that continues forever? Despite using break of
course. I ask, because your suggestion works, but unfortunately uses
100% of my CPU power. :-( Well, it works, but is there maybe another way
to do the same without using so much of my resources? :-) That would be
great.


Use time.sleep().

-Peter
Jul 18 '05 #5
Tom
Peter Hansen wrote:
Use time.sleep().

-Peter

If I use sleep, I have to know how long the process should sleep, but
sometimes it should sleep for seconds and sometimes for minutes. So it
would be good if I can put it to sleep and somehow wake it up if the
file changed.

Tom

Jul 18 '05 #6
Tom wrote:
Peter Hansen wrote:
Use time.sleep().

-Peter

If I use sleep, I have to know how long the process should sleep, but
sometimes it should sleep for seconds and sometimes for minutes. So it
would be good if I can put it to sleep and somehow wake it up if the
file changed.


If you use something like::

ctime = os.stat(file).s t_ctime
while True:
if os.stat(file).s t_ctime > ctime:
break
time.sleep(1.0)
# Do stuff...

The load on your CPU will be negligible and your program will wake up at
most 1 second after the modification

-tim

Jul 18 '05 #7
Tom wrote:

Peter Hansen wrote:
Use time.sleep().

-Peter

If I use sleep, I have to know how long the process should sleep, but
sometimes it should sleep for seconds and sometimes for minutes. So it
would be good if I can put it to sleep and somehow wake it up if the
file changed.


See Tim's excellent reply.

Also, note that there are non-cross-platform solutions that basically
notify your program "as soon as" (possibly without any latency guarantees)
a change has happened. They are harder to use and quite likely unnecessary
in your case, and the time.sleep() approach Tim points out is simpler
and therefore the best thing to start with.

You do have to confirm of course that this other application won't
be writing to the file so often that you might run into trouble.

Does this other program write updates infrequently, and does your
Python analysis script run quickly enough, that if your code wakes
up within a few seconds of each update it will complete its work
soon enough to avoid problems?

-Peter
Jul 18 '05 #8
Hi,

What's the problem?

Just wait as long as you can accept to be "late" when your file changes.

So if you can accept e. g. 5 seconds delay, you'll change Lane's example to:

time=os.stat(fi le)[8]

# analyze data

while 1:
if os.stat(file)[8]>time:
#analyze data again
else:
time.sleep(5)
This will drastically reduce your CPU load ;-)

hth

Werner

Tom wrote:
Peter Hansen wrote:
Use time.sleep().

-Peter

If I use sleep, I have to know how long the process should sleep, but
sometimes it should sleep for seconds and sometimes for minutes. So it
would be good if I can put it to sleep and somehow wake it up if the
file changed.

Tom


Jul 18 '05 #9
In article <3F************ ***@engcorp.com >, Peter Hansen wrote:

Also, note that there are non-cross-platform solutions that basically
notify your program "as soon as" (possibly without any latency guarantees)
a change has happened. They are harder to use and quite likely unnecessary
in your case, and the time.sleep() approach Tim points out is simpler
and therefore the best thing to start with.


Here's one that uses PythonWin and WMI. I wrote this awhile ago because I
was experimenting with writing a templating system that would process input
files "on the fly" whenever they changed. It seems to drive my CPU usage up,
though - not sure why, because I'm pretty sure that events.NextEven t()
blocks until an event comes. Anyway, I don't know if this is useful to
anyone, but here it is... (apologies for the wide margins):

import time, win32com; time.sleep(1)
from win32com.client import GetObject

interval = 1
filenames = [r'c:\test.txt', r'c:\output.txt ']

class FileChangeEvent :
def __init__(self, filename, new_size, old_size):
self.filename = filename
self.new_size = new_size
self.old_size = old_size

def on_change(event ):
print event.__dict__

wmi = GetObject(r'win mgmts:{imperson ationLevel=impe rsonate}!\\.\ro ot\cimv2')
filespec = ' OR '.join(["TargetInstance .Name='%s'" % filename.replac e('\\', '\\\\') for filename in filenames])
query = "SELECT * FROM __InstanceModif icationEvent WITHIN %d WHERE TargetInstance ISA 'CIM_DataFile' AND (%s)" % (interval, filespec)
events = wmi.ExecNotific ationQuery(quer y)

while True:
event = events.NextEven t()
on_change(FileC hangeEvent(str( event.TargetIns tance.Name),
int(event.Targe tInstance.FileS ize),
int(event.Previ ousInstance.Fil eSize)))

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3579
by: Jeronimo Bertran | last post by:
I have a multithreaded application that manipulates a Queue: protected Queue outputQueue; when I add elements to the queue I use a critical section private void Add(OutputRecord record) { lock(outputQueue) {
22
2572
by: codefixer | last post by:
Hi, I have a situation where I have to handle the following scenario. The main() must wait for child to complete or the main() must kill the child after 3 seconds and exit. /* Assume everythign is declared */ time(&start); while ( childpid != wait(&status) )
6
5713
by: Jeremy Chapman | last post by:
I have a button on my page which when clicked redirects to another page. on the page load of the page that I've redirected to, there is a long query. What I want to do is with dhtml, display test in the center of the screen saying 'please wait..' so that the text displays until the new page is returned. My problem is that the html is not visually updated because the browser is busy waiting for the http request to complete. Is there any...
11
6970
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then making the main thread sleep until the counter gets back to zero. The call back function for each call decrements the counter. Is there a better way to make the thread wait until all calls are complete besides using the counter? I've seen some things...
18
51025
by: Coder | last post by:
Howdy everybody! How do I do the following... while (myVary != true){}; Obviously I do not want to use 100% of the processor to stay in this infinite loop till myVar == true. But wait do I do? Thanks yal!
8
37736
by: C.Joseph Drayton | last post by:
Hi All, I am calling a PHP script that does an operation then sends back a result. I want JavaScript to wait until that result has been recieved. I am using the following code. What can I do to stop it from generating a 'Too much recursion' error? function WaitForData() {
12
5277
by: Perecli Manole | last post by:
I am having some strange thread synchronization problems that require me to better understand the intricacies of Monitor.Wait/Pulse. I have 3 threads. Thread 1 does a Monitor.Wait in a SyncLock block protecting a resource. Thread 2 and 3 also have a SyncLock block protecting the same resource and after executing some code in their blocks they both do a Monitor.Pulse to hand of the locked resource back to thread 1. While thread 1 has...
1
5690
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for frmRandom, i tried it with the Sleep API and a Do/Until Loop and neither worked, could you please help me, I have my code below. frmMachine 'Slot Machine, (c) 2006 Peter Browne, GPL Licensed ' 'Peter Browne 'Sheridan Corporate Centre '2155 Leanne...
2
3489
by: greyradio | last post by:
I've recently have been given an assignment to do and it seems that notify() does notify() any of the waiting threads. The project entails 10 commuters and two different toll booths. The EZPass booth allows a commuter to pass easily while a cash booth creates a wait line. The wait line is expected to wait for the 5th commuter to pass before any commuter on the wait line can pass through the toll booth. This is where my problem lies. All the...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.