File in use problem

  • Thread starter Thread starter Armin Zingler
  • Start date Start date
A

Armin Zingler

Kristoffer Persson said:
I have a FileSystemWatcher in my application that triggers on file
(*.xml) creation and renaming in a folder. When I detect a new file I
should read it, but sometimes it will fail (file in use).

It is an FTP server that writes the file, so it will create the file
and write to it little by little. So after I detect the file, it can
be a while (time depending on the connection) before it is
complete.

QUESTION:
When the file is detected, I want to make sure it is complete and
free to use before I pass it on to the reading function. HOW do I
accomplish that? I have tried two ways:

1. Open it in a FileShare.None mode.
This didn't work, since it actually succeeded sometimes, causing the
FTP server some writing problems, since _I_ locked the file for
_HIM_.

2. Monitoring changes in file size.
Didn't work since the file size reported was the file size for a
completely downloaded file and not the partial download.

Any ideas?

- Kristoffer -


No way. Try to open the file in the file mode you need. If you succeed it's
ok, if not, it failed. Sounds simple, but that's how it is. I guess if file
access succeeds, the file has been written completely, but there's no
guarantee. If the FTP server is "clever", it should lock the file til it's
written completely, so succeeding in opening it should be a sign that it's
ready to be processed. But, let's assume the file is created first, then
written into it and it gets closed between the two steps even for a very
short period of time. If you happen to open it just between these two steps,
you've got a problem. So, you must hope for a well designed application
writing the file.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
I have a FileSystemWatcher in my application that triggers on file (*.xml)
creation and renaming in a folder. When I detect a new file I should read
it, but sometimes it will fail (file in use).

It is an FTP server that writes the file, so it will create the file and
write to it little by little. So after I detect the file, it can be a while
(time depending on the connection) before it is complete.

QUESTION:
When the file is detected, I want to make sure it is complete and free to
use before I pass it on to the reading function. HOW do I accomplish that? I
have tried two ways:

1. Open it in a FileShare.None mode.
This didn't work, since it actually succeeded sometimes, causing the FTP
server some writing problems, since _I_ locked the file for _HIM_.

2. Monitoring changes in file size.
Didn't work since the file size reported was the file size for a completely
downloaded file and not the partial download.

Any ideas?

- Kristoffer -
 
Hi Kristoffer,

Normally I say do not use this for this purpose however you say
When I detect a new file I should read it, but sometimes it will fail (file
in use).

In this case I would make a try, catch and end try with a counter in it and
maybe even something as a *threading.thread.sleep(5000)* in it.

It is maybe dirty however you say , "sometimes" it will fail.

Just my idea about it.

Cor
 
* "Kristoffer Persson said:
When the file is detected, I want to make sure it is complete and free to
use before I pass it on to the reading function. HOW do I accomplish that? I
have tried two ways:

1. Open it in a FileShare.None mode.
This didn't work, since it actually succeeded sometimes, causing the FTP
server some writing problems, since _I_ locked the file for _HIM_.

Try to open it /with/ file sharing.
 
Herfried K. Wagner said:
Try to open it /with/ file sharing.

Then I wouldn't get the whole file.. OK, I can open it and the FTP server
can write it, but I don't want it until it's complete.

- Kristoffer -
 
Cor Ligthert said:
Hi Kristoffer,

Normally I say do not use this for this purpose however you say
Why?
(file
in use).

In this case I would make a try, catch and end try with a counter in it and
maybe even something as a *threading.thread.sleep(5000)* in it.

That is what I'm doing now, which is not working...

The thing is that I need to make sure that the file is OK to work with
before I send it to the "file content reader". I should not do the check in
the reader, since it is not my code...

- Kristoffer -
 
Any ideas?

Do you have any control over the ftp process? I would reccommend
downloading the file under a temporary name and then rename the file to the
correct name after downloading. That way, the FileSystemWatcher will not
attempt to process the file until it is completely downloaded.
 
Back
Top