473,378 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to find out if another process is using a file

I'm writing a program which reads a series of data files as they are dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.

I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for now
and come back to it later. This needs to work on linux and windows. Mac
OS would be a bonus too. An os-independent solution would be good, but I
could write os-specific options and have it pick the appropriate one.

Is there any way of doing this? I've drawn a blank with google so far.

A nasty hack would be to use the file modification time, and wait until
that's a few seconds in the past, but is there a nice solution?
--
I'm at CAMbridge, not SPAMbridge
Jan 18 '07 #1
8 4950
js
How about using lock?
Let writing process locks the files before writing, and unlock after
the job's done.

I think it'd work file in most environment.

On 1/19/07, Tom Wright <te***@spam.ac.ukwrote:
I'm writing a program which reads a series of data files as they are dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.

I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for now
and come back to it later. This needs to work on linux and windows. Mac
OS would be a bonus too. An os-independent solution would be good, but I
could write os-specific options and have it pick the appropriate one.

Is there any way of doing this? I've drawn a blank with google so far.

A nasty hack would be to use the file modification time, and wait until
that's a few seconds in the past, but is there a nice solution?
--
I'm at CAMbridge, not SPAMbridge
--
http://mail.python.org/mailman/listinfo/python-list
Jan 18 '07 #2
"Tom Wright" <te***@spam.ac.ukescribió en el mensaje
news:eo**********@gemini.csx.cam.ac.uk...
I'm writing a program which reads a series of data files as they are
dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.

I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for
now
and come back to it later. This needs to work on linux and windows. Mac
OS would be a bonus too. An os-independent solution would be good, but I
could write os-specific options and have it pick the appropriate one.
Use os.open with the O_EXCL flag; will fail if the other process has the
file still open (and will fail if another process is reading the file, too,
not just if someone is writing).

--
Gabriel Genellina
Jan 18 '07 #3
In article <ma***************************************@python. org>,
"Gabriel Genellina" <ga******@yahoo.com.arwrote:
"Tom Wright" <te***@spam.ac.ukescribió en el mensaje
news:eo**********@gemini.csx.cam.ac.uk...
I'm writing a program which reads a series of data files as they are
dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.

I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for
now
and come back to it later. This needs to work on linux and windows. Mac
OS would be a bonus too. An os-independent solution would be good, but I
could write os-specific options and have it pick the appropriate one.

Use os.open with the O_EXCL flag; will fail if the other process has the
file still open (and will fail if another process is reading the file, too,
not just if someone is writing).
O_EXCL fails if the file exists at all - whether closed or open.

Donn Cave, do**@u.washington.edu
Jan 18 '07 #4

In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.eduwrites:
|In article <ma***************************************@python. org>,
| "Gabriel Genellina" <ga******@yahoo.com.arwrote:
| "Tom Wright" <te***@spam.ac.ukescribió en el mensaje
| news:eo**********@gemini.csx.cam.ac.uk...
|
| I'm writing a program which reads a series of data files as they are
| dumped
| into a directory by another process. At the moment, it gets sporadic bugs
| when it tries to read files which are only partially written.
| >
| I'm looking for a function which will tell me if a file is opened in
| write-mode by another process - if it is, my program will ignore it for
| now
| and come back to it later. This needs to work on linux and windows. Mac
| OS would be a bonus too. An os-independent solution would be good, but I
| could write os-specific options and have it pick the appropriate one.
|
| Use os.open with the O_EXCL flag; will fail if the other process has the
| file still open (and will fail if another process is reading the file, too,
| not just if someone is writing).
|>
|O_EXCL fails if the file exists at all - whether closed or open.

Yes. In theory. In practice, it usually works on normal files, provided
that all opens are local. Under some circumstances, it will even work
for NFS mounted files, as far as I recall.

MVS (now zOS) and other mainframe systems had what the poster wants, but
modern systems don't. I shall not recommend that the poster asks IBM
for a copy of zOS, for reasons that will be well-known to anyone who used
MVS (which are NOT the ones claimed by the Unix brigade, which are mostly
bogus) :-)

Under Linux, you can do something with fuser, and I am pretty certain that
modern Macintoshes (i.e. BSD) will have an equivalent. It can't be made
reliable (unlike under MVS), but might reduce the number of problems.
Regards,
Nick Maclaren.
Jan 18 '07 #5
In article <eo**********@gemini.csx.cam.ac.uk>,
nm**@cus.cam.ac.uk (Nick Maclaren) wrote:
In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.eduwrites:
|In article <ma***************************************@python. org>,
| "Gabriel Genellina" <ga******@yahoo.com.arwrote:
| "Tom Wright" <te***@spam.ac.ukescribió en el mensaje
| news:eo**********@gemini.csx.cam.ac.uk...
| Use os.open with the O_EXCL flag; will fail if the other process has the
| file still open (and will fail if another process is reading the file,
| too,
| not just if someone is writing).
|>
|O_EXCL fails if the file exists at all - whether closed or open.

Yes. In theory. In practice, it usually works on normal files, provided
that all opens are local. Under some circumstances, it will even work
for NFS mounted files, as far as I recall.
Mm, by "fail", I meant

An attempt to open with O_EXCL set will "fail" if the file exists at all,
i.e., the file will not be opened, a negative value will be returned,
and errno will be set to EEXIST.

What I neglected to mention is that this effect obtains when O_EXCL
is used in combination with O_CREAT. Without O_CREAT, O_EXCL doesn't
mean anything and is ignored.

If there is any significant difference between theory and practice
in this matter, it's news to me.

Donn
Jan 18 '07 #6

In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.eduwrites:
| |>
| |O_EXCL fails if the file exists at all - whether closed or open.
|
| Yes. In theory. In practice, it usually works on normal files, provided
| that all opens are local. Under some circumstances, it will even work
| for NFS mounted files, as far as I recall.
|>
|Mm, by "fail", I meant
|>
|An attempt to open with O_EXCL set will "fail" if the file exists at all,
|i.e., the file will not be opened, a negative value will be returned,
|and errno will be set to EEXIST.
|>
|What I neglected to mention is that this effect obtains when O_EXCL
|is used in combination with O_CREAT. Without O_CREAT, O_EXCL doesn't
|mean anything and is ignored.
|>
|If there is any significant difference between theory and practice
|in this matter, it's news to me.

Actually, it is undefined behaviour in POSIX, and I have used a Unix
where O_EXCL on its own failed if the file existed (of course, it also
failed if it DIDN'T exist, because of the lack of O_CREAT). I can't
remember which - perhaps AIX or OSF/1 (not DEC).

But I was referring to a different problem. Some remote and parallel
filing systems handle such things very badly, and it is often possible
to create a file even if it exists and O_CREAT|O_EXCL is set. A similar
remark applies to 'special' files, even on fairly mainstream, local
filing systems.
Regards,
Nick Maclaren.
Jan 18 '07 #7
js wrote:
How about using lock?
Let writing process locks the files before writing, and unlock after
the job's done.
Is locking mandatory or co-operative? I don't have any control over the
process which is doing the writing, so if it's co-operative it's no good to
me.

If it's mandatory, then I can try to acquire a lock on the file - if this
fails or blocks, then the other process must have it open. Will this work?
Jan 19 '07 #8
Tom Wright wrote:
js wrote:
>How about using lock?
Let writing process locks the files before writing, and unlock after
the job's done.

Is locking mandatory or co-operative? I don't have any control over the
process which is doing the writing, so if it's co-operative it's no good
to me.

If it's mandatory, then I can try to acquire a lock on the file - if this
fails or blocks, then the other process must have it open. Will this
work?
AFAIK it's cooperative.

Diez
Jan 19 '07 #9

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

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
1
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.  In each directory, I need to process each image and generate an HTML file listing all of the images and links to the subdirectories....
2
by: Sri | last post by:
I am writing an asp.net applicaition using VB coding. In a function, I am opening an excel file with the following code, Dim objExcel As Object Dim objWorkBook As Object objExcel =...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
5
by: su | last post by:
to find which process dumped core at the promt we give $ file core.28424 core.28424: ELF 32-bit LSB core file of 'soffice.bin' (signal 11), Intel 80386, version 1 (SYSV), from 'soffice.bin' ...
9
by: Carl | last post by:
Hi Is it possible to find out if another process is using a paticular file (xml)? There does not seem to be anything useful in the File och Filestream class for this purpose. regards Carl
3
by: moongirl | last post by:
Part of an application I am building includes a button which the user can click on to view a PDF file in Acrobat Reader, using the following code: System.Diagnostics.Process.Start(@"C:\Program...
3
by: dittytwo | last post by:
Hi all I have been looking around the web and can't seem to find a solution the solution that i have found and manipulated seems to bring back the whole list of currently running process's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.