B4J Question Concurrent reading of file in WebApp

hatzisn

Well-Known Member
Licensed User
Longtime User
I am trying to implement a WebApp that performs a certain task. In order to avoid recompiling the app with each new configuration I am planning to create a sub in the class that handles the request, that uses a File.ReadMap and add the new configurations in there each time.

My Concern is if there are two requests at the same time how the operating system will handle the code? Is it able for concurrent reading of the file?
 
Last edited:

hatzisn

Well-Known Member
Licensed User
Longtime User
I tried to pre-fix this with this code and it does not return anything (but without resumable sub and no TRY it works). What am I doing wrong?

B4X:
Sub GetConfig(PPValue As String) As ResumableSub
    Try
        If File.Exists(File.DirData("TaskHandler"), "config.dat") Then
            '
        Else
            File.WriteMap(File.DirData("TaskHandler"), "config.dat", CreateMap("ffc":"111"))
        End If
 
        Dim m As Map = File.ReadMap(File.DirData("TaskHandler"), "config.dat")
        If m.Get(PPValue) <> Null Then
            Return m.Get(PPValue)
        Else
            Return "none"
        End If
    Catch
        'Log(LastException)
        Sleep(300) 'Wait 300 millis and retry
        wait for (GetConfig(PPValue)) Complete (sRet As String)
        Return sRet
    End Try
 
End Sub
 
Upvote 0

Pendrush

Well-Known Member
Licensed User
Longtime User
I tried to pre-fix this with this code and it does not return anything (but without resumable sub and no TRY it works). What am I doing wrong?

B4X:
Sub GetConfig(PPValue As String) As ResumableSub
    Try
        If File.Exists(File.DirData("TaskHandler"), "config.dat") Then
            '
        Else
            File.WriteMap(File.DirData("TaskHandler"), "config.dat", CreateMap("ffc":"111"))
        End If
 
        Dim m As Map = File.ReadMap(File.DirData("TaskHandler"), "config.dat")
        If m.Get(PPValue) <> Null Then
            Return m.Get(PPValue)
        Else
            Return "none"
        End If
    Catch
        'Log(LastException)
        Sleep(300) 'Wait 300 millis and retry
        wait for (GetConfig(PPValue)) Complete (sRet As String)
        Return sRet
    End Try
 
End Sub

B4X:
Sleep(300) 'Wait 300 millis and retry
wait for (GetConfig(PPValue)) Complete (sRet As String)
Return sRet

These 3 lines will be executed ONLY if you have some error in TRY, as you put them in CATCH.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
B4X:
Sleep(300) 'Wait 300 millis and retry
wait for (GetConfig(PPValue)) Complete (sRet As String)
Return sRet

These 3 lines will be executed ONLY if you have some error in TRY, as you put them in CATCH.

Yes, that was the intention if concurrent reading could not be executed. Since it does I changed this sub with the incredients of only the try part of try - catch.
 
Upvote 0
Top