Concurrency issue with files

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

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Concurrency issue with files

Postby 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?
yuzutake
 
Posts: 7
Joined: Mon Feb 27, 2017 5:36 pm

Re: Concurrency issue with files

Postby 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
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
 
Posts: 5256
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles

Re: Concurrency issue with files

Postby 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!
Livecode programming until the cat hits the fan ...
AxWald
 
Posts: 280
Joined: Thu Mar 06, 2014 2:57 pm

Re: Concurrency issue with files

Postby 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
yuzutake
 
Posts: 7
Joined: Mon Feb 27, 2017 5:36 pm


Return to Windows

Who is online

Users browsing this forum: No registered users and 1 guest