Concurrency issue with files

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
yuzutake
Posts: 10
Joined: Mon Feb 27, 2017 5:36 pm

Concurrency issue with files

Post by yuzutake » Tue Mar 07, 2017 5:34 pm

Here's my problem:
I'll have several computers (not always connected to the network, not always the same IP...) that will do some work on the same network resource. They must not do it at the same time. I'm wondering if there's a simple way to know whether a process is already doing its work.

I tried to make a lock file with this draft:

Code: Select all

on mouseUp
   put specialFolderPath("desktop")&"/myLock.txt" into lockName
   
   if the short name of the target is "btnLck" then
      if fld "fldRes" is "locked by me" then exit mouseUp
      if there is a file lockName then
         put "locked by another process" into fld "fldRes"
      else
         open file lockName for write
         put "locked by me" into fld "fldRes"
      end if
      
   else if the short name of the target is "btnRel" and fld "fldRes" is "locked by me" then
      close file lockName
      delete file lockName
      put "released" into fld "fldRes"
   end if
end mouseUp

on preOpenStack
   put empty into fld "fldRes"
end preOpenStack
but it isn't safe.
I also tried to see if the result of open file told us "file locked" (as MS Word does) but it doesn't :(

Am I forced to make a DLL and use Windows API?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Concurrency issue with files

Post by FourthWorld » Tue Mar 07, 2017 9:50 pm

I've found that many OS conventions vary across platforms, and even on a single platform may have exceptions not easily accounted for. So for network applications I tend to use lock files.
https://en.wikipedia.org/wiki/File_locking#Lock_files
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Concurrency issue with files

Post by AxWald » Wed Mar 08, 2017 10:56 am

Hi,

I'm using this:

Code: Select all

-- gURLToPut = destination path
-- gLockFile = filename of a local dummy lock file
-- cmd writelog = a cmd that writes to a log file
-- function getMyTS() = a function returning a timestamp

on mouseUp 
  put false into GoOn
   repeat until GoOn
      put SetLock() into GoOn
      wait 5 sec with messages      
   end repeat
   
   -- action code here
   
   delete URL (gURLToPut & gLockFile)
   writelog "  # Lock deleted #"
   get the result
   if it is not empty then writelog "Error@LockDelete: " & it
end mouseUp


function SetLock
   put URL gURLToPut into MyVar
   if gLockFile is in MyVar then
      writelog "  # FTP is locked #"
      return false
   else
      Put "./" & gLockFile into TheLockFile
      --  optional, for better analysis in case of problems:  --
      open file TheLockFile for write
      write the short name of this stack & " | " & getMyTS() to file TheLockFile
      close file TheLockFile
      --  /optional  --
      put theLockFile into URL (gURLToPut & gLockFile)
      writelog "  # Created Lock #"
      return true
   end if
end SetLock
I don't use this in a heavy concurrency environment - so there may be quirks. But so far it works. Hope I didn't remove something important when shortening for posting ;-)

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

yuzutake
Posts: 10
Joined: Mon Feb 27, 2017 5:36 pm

Re: Concurrency issue with files

Post by yuzutake » Thu Mar 09, 2017 11:03 am

If Process #1 goes to sleep just before "put theLockFile into URL (gURLToPut & gLockFile)", "gLockFile is in MyVar" isn't true yet for Process #2.

I've looked at other forums and I'll do

Code: Select all

on mouseUp
   put empty into fld "fldLock"
   set the hideConsoleWindows to true
   open process "testlock.cmd" for text update
   read from process "testlock.cmd" for 1 char in 1 seconds
   if it is empty then
      put "locked by me" into fld "fldLock"
   else
      put "locked by another" into fld "fldLock"
      close process "testlock.cmd"
   end if
end mouseUp
where testlock.cmd looks like

Code: Select all

@echo off
cmd >"%temp%\myLock.txt"
It's still not perfect since the testlock.cmd process can be killed thus unlocking %temp%\myLock.txt... But it's OK for me.
Thanks

Post Reply

Return to “Windows”