check if other routine is accessing a text file [message #88222] |
Sun, 30 March 2014 18:26  |
hoiting2008
Messages: 4 Registered: March 2014
|
Junior Member |
|
|
Hi,
I'm trying to read/write (openw,openu,openr) the same text file using multiple IDL prompts. The different prompts usually read/write the file at different time, but there is a tiny chance that they read/write at the same time. Is there any keywords or routines that can test if other program is using the file?
Ideally, I'd like to do something like this:
test_if_someone_writing?
yes: wait for 3 seconds and try again
no: go ahead and read/write
Thanks!
|
|
|
Re: check if other routine is accessing a text file [message #88223 is a reply to message #88222] |
Sun, 30 March 2014 19:42   |
Jim Pendleton
Messages: 165 Registered: November 2011
|
Senior Member |
|
|
On Sunday, March 30, 2014 7:26:54 PM UTC-6, hoiti...@gmail.com wrote:
> Hi,
>
>
>
> I'm trying to read/write (openw,openu,openr) the same text file using multiple IDL prompts. The different prompts usually read/write the file at different time, but there is a tiny chance that they read/write at the same time. Is there any keywords or routines that can test if other program is using the file?
>
>
>
> Ideally, I'd like to do something like this:
>
>
>
> test_if_someone_writing?
>
> yes: wait for 3 seconds and try again
>
> no: go ahead and read/write
>
>
>
> Thanks!
There is nothing in IDL's file I/O library that will do this directly. You might consider using a semaphore around your openw and openr statements. See the docs for SEM_CREATE, SEM_LOCK, SEM_RELEASE, and SEM_DELETE. They're intended primarily to prevent conflicts and collisions on system resources, disk files included.
Jim P.
|
|
|
Re: check if other routine is accessing a text file [message #88224 is a reply to message #88223] |
Mon, 31 March 2014 08:14  |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Sunday, March 30, 2014 8:42:27 PM UTC-6, Jim P wrote:
> On Sunday, March 30, 2014 7:26:54 PM UTC-6, hoiti...@gmail.com wrote:
>
>> Hi,
>
>>
>
>>
>
>>
>
>> I'm trying to read/write (openw,openu,openr) the same text file using multiple IDL prompts. The different prompts usually read/write the file at different time, but there is a tiny chance that they read/write at the same time. Is there any keywords or routines that can test if other program is using the file?
>
>>
>
>>
>
>>
>
>> Ideally, I'd like to do something like this:
>
>>
>
>>
>
>>
>
>> test_if_someone_writing?
>
>>
>
>> yes: wait for 3 seconds and try again
>
>>
>
>> no: go ahead and read/write
>
>>
>
>>
>
>>
>
>> Thanks!
>
>
>
> There is nothing in IDL's file I/O library that will do this directly. You might consider using a semaphore around your openw and openr statements. See the docs for SEM_CREATE, SEM_LOCK, SEM_RELEASE, and SEM_DELETE. They're intended primarily to prevent conflicts and collisions on system resources, disk files included.
>
>
>
> Jim P.
Jim's suggestion is a good one. Another possibility is to create another copy of the file (file_copy), write all your data to it, and then use file_move to give it the correct name. That way you don't need to worry about writing new data while the file is still being read in another IDL process. You would probably need to put a catch (or on_ioerror?) to make sure the file_move didn't fail.
Cheers,
Chris
|
|
|