Announcement

Collapse
No announcement yet.

concurrent append access for output file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • concurrent append access for output file

    This is probably asking for too much, but there isn't a way to open the same sequential output file in append mode by 2 instances of a PB program is there? I'm thinking the only way I can pull that off is to go to a DB table instead.

    I call a routine I wrote that does custom spam analysis for incoming email. My may server can call a routine and hand it off to the PB routine for review. If it doesn't get deleted, it probably wasn't spam.

    --Ben

  • #2
    Try "OPEN ... LOCK SHARED" and retries if the file is currently locked for writing by another instance.

    Comment


    • #3
      You don't even need to SHARE... you just need to OPEN exclusive, write and CLOSE.

      Note that when you OPEN the file may be in use by another instance, so you will need some kind of retry loop when the OPEN fails because it is in use.

      Code:
      FUNCTON AppendToFile (filename$, TheData$)  AS LONG
        LOCAL hFile AS INTEGER
        ON ERROR GOTO WaitUp
      
        hFile = FREEFILE
         OPEN filename$ for APPEND AS hFile
         PRINT #hFile, theData$
         CLOSE hFile
         FUNCTION =  1   ' true = success
         EXIT FUNCTION
      
      WaitUp:
      DELAY 250
      RESUME
      .
      END FUNCTION
      "Typically" you would add some kind of retry counter and bail out if/when there are "too many" tries,,, enough to indicate there is really something wrong.

      FWIW, this would be a heck of lot easier under Windows as you could queue the write requests using simple system synchronization objects. (You did realize you posted in the PB/DOS forum, right?) So it you are looking for an excuse to "EXIT MS-DOS" here it is.

      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Thanks! I have both versions but started this exercise in the DOS version. Just haven't converted it to the Windows variant yet.

        --Ben

        Comment


        • #5
          Learned file input/output statements are not thread safe (if you ever get into that) from Michael Mattias.
          Also, definitely want exclusive access to a sequential file as Michael has shown.
          Code:
          FUNCTION PBMAIN AS LONG
           errcode& = AppendRecord("Junk.txt",TIME$)
           ? ERROR$(errcode&),,"AppendRecord"
          END FUNCTION
          
          FUNCTION AppendRecord(sFileName AS STRING,sdata AS STRING) THREADSAFE AS LONG
          
           LOCAL x          AS LONG
           LOCAL MaxRetry   AS LONG
           LOCAL RetrySleep AS LONG
           LOCAL hfile      AS LONG
          
           MaxRetry   = 50
           RetrySleep = 100
          
           hFile = FREEFILE
           FOR x = 1 TO MaxRetry
            OPEN sFileName FOR APPEND AS hFile 'open exclusive for append
            IF ERR = 0 THEN                    'success
             PRINT #hfile,sdata                'write
             CLOSE #hfile                      'close file
             EXIT FOR                          'exit
            ELSE                               'could not open
             SLEEP RetrySleep                  'wait a bit
            END IF
           NEXT                                
           FUNCTION = ERR                      'return possible error code or 0
          END FUNCTION
          The world is full of apathy, but who cares?

          Comment


          • #6
            Mike D...
            Learned file input/output statements are not thread safe...
            Um, additional threads of execution are not a consideration under MS-DOS.

            Thanks! I have both versions but started this exercise in the DOS version. Just haven't converted it to the Windows variant yet.
            This is a great example.. if you write it for MS-DOS and then "convert" it to Windows by just changing the relevant verbs and functions you are really doing a poor job of converting because under Windows there are better ways of doing this. (Do a search on the word "queue" in the Source code forum...there's even lots of different ways to do it via queuing under Windows!)

            When you see me refer to "mindless conversion of verbs, functions and datatypes" when a thread subject is "porting" to a new language or operating system .. this is exactly the kind of thing I mean.

            MCM

            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Just FWIW,,if you are interested in "concurrent access" to files in general, here's a tutgorial on my website:

              "Fundamentals Of Multi-User Programming." Article published in December 1995 issue of "BASICally Speaking" magazine discussing the principles of writing multi-user programs; code samples in BASIC. Rich Text format; placed in the Public Domain June 2005
              http://www.talsystems.com/tsihome_ht...rogramming.rtf

              All the code is in PB-DOS but the general methods shown are equally applicable to the Windows environment.



              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                In post 4 Ben specifically said DOS. If he, or anyone, wants to open the same file more than once at a time in a PBWin or PBCC app I suggest getting another # nFile with OPEN HANDLE.

                I'm pretty sure that won't work in DOS, but it has been a vveerryy long time since I had PB DOS to check with.

                Cheers,
                Dale

                Comment


                • #9
                  I'm pretty sure that won't work in DOS, but it has been a vveerryy long time since I had PB DOS to check with.
                  Multiple handles to the same file may be used in PB/DOS (and PB/Windows). Actually that is a pretty good way to write a program to test multiple users if your hardware setup does not otherwise support it.

                  I had a demo of using multiple handles to simulate "going back" in a file opened for sequential access... but darned if I know where I put it. But something like you get a variable number of records per logical transaction but you cannot process until you know how many. Something like

                  Code:
                    h1 = FREEFILE
                    OPEN filename FOR INPUT AS h1
                    h2 = FREEFILE
                    OPEN Filename FOR INPUT AS h2    ' OPEN SAME FILE under a different handle
                    DO
                     LINE INPUT h1, TheRecord$
                     IF TheRecord$ =   Header Record THEN   ' count until some "trigger"  for start or end transaction occurs
                         IF nRecsThisTrans > 0 THEN
                          FOR Z =  1 TO nRecsThisTrans
                               Line  INPUT h2, The Record$                 <<< read on 'h2'  which is still at start of transaction
                               CALL ProcessARecord (TheRecord$)
                           NEXT
                              nRecsThisTrans= 0                 ' reset
                          END IF                                       ' if start of new transaction
                       INCR nRecsThisTrans
                    LOOP UNTIL EOF (h1)
                  ...
                  Something like that. Essentially "h1" is used only to get a record count of the current transaction... h2 is used to actually process transactions.

                  This is generally not a big deal under Windows, since you can just read records and save them to memory until the transaction is complete and fully loaded to memory.... but under MS-DOS, the available memory is far less and you sometimes cannot fit the transaction in memory and you have to process record-by-record.

                  I did a great version of this for Blue Cross Blue Shield of Wisconsin on an IBM mainframe with COBOL.. but the same principle was used... use one file handle to count how many records to process and a second handle to actually process.

                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    FREEFILE in PBDOS????
                    Dale

                    Comment


                    • #11
                      Hi Dale,

                      Yes, FREEFILE is a valid function in PBDOS.

                      Comment


                      • #12
                        BTW..

                        If he, or anyone, wants to open the same file more than once at a time in a PBWin or PBCC app I suggest getting another # nFile with OPEN HANDLE.
                        While not relevant to the MS-DOS environment, that is not how OPEN HANDLE is used.(Although you "could" use it here, you'd have to go thru the extra step of using FILEATTR() to get the required system handle).

                        But here is a demo showing one way OPEN HANDLE IS used...

                        Create and use a file which will be deleted on close 4/10/08
                        FULL THREAD AT
                        https://forum.powerbasic.com/showthread.php?t=37059
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Quote out of context. I did say not for DOS.
                          Yes, PB Help even says use FILEATTR
                          I don't think you even read your own 13 year old post.

                          Didn't read quoted post, Help or own post!!! Uh huh
                          Dale

                          Comment


                          • #14
                            Originally posted by Dale Yarker View Post
                            Quote out of context. I did say not for DOS.
                            Yes, PB Help even says use FILEATTR
                            I don't think you even read your own 13 year old post.

                            Didn't read quoted post, Help or own post!!! Uh huh
                            And as is frequently the case, pointed to code which is:
                            "Not tested, not even compiled"

                            Comment


                            • #15
                              I read "PC/CC or PB/WIN" and quoted that.

                              My post was to improve on your suggestion to use OPEN HANDLE.

                              I only referenced MS-DOS because that is what this forum is.
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment

                              Working...
                              X