B4J Question HttpJob in BackGroundWorker compared to PyBridge server example. Benchmark. Is it correct?

hatzisn

Expert
Licensed User
Longtime User
Good evening to all of you,

I was exploring the PyBridge server example and I was wondering If we do the same with an HttpJob in the worker class then what happens in the background worker class. Is the worker thread considered busy until it returns the answer in order to serve an other handler's function calling? I checked this out with multiple requests and it seems that it remains busy in the same thread until it finishes the httpjob and return the value. I do not know if my methodology is correct and I am not a hundred percent sure if what I have observed is correct. I am writting bellow the code of the multiple requests console app and I attach the benchmarking application. What confuses me is the order of the job results in relation to the datetime now they were initiated and returned - it is an ID of course of order... Is this correct?

(Start the "Check1Thread" in debug mode to emulate latency and the code bellow in release mode. Then compare it to what happens if we start both in release mode. What is wrong in this case?????? Does it show that it does not work as expected?)

B4X:
Sub Process_Globals
    Dim iCounter As Int = 0
End Sub

Sub AppStart (Args() As String)
    Log("Making Multiple Requests!!!")
    MakeMultipleRequests
    StartMessageLoop
End Sub

Sub MakeMultipleRequests
    For ii = 1 To 50
        Dim hj As HttpJob
        hj.Initialize("jb" & ii, Me)
        hj.Download("http://localhost:51042/checkone")
    Next
End Sub

Sub JobDone(hj As HttpJob)
    iCounter = iCounter + 1
    Log(iCounter & "--" & hj.JobName & "==" &  hj.GetString)
    If iCounter = 50 Then
        StopMessageLoop
    End If
End Sub
 

Attachments

  • Check1Thread.zip
    3.5 KB · Views: 5

Erel

B4X founder
Staff member
Licensed User
Longtime User
The worker is not considered busy while the http request is processed. There is an underlying thread pool that manages the network communication.

You must test it in release mode as in debug mode the servlet handler (MyXThread) is run by a single thread and will be blocked by the internal message loop.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User

Thank you for the answer. So if I get it right, if I wanted the http jobs to be executed sequentially then the only way is to take advantage of the blocking single thread executing the handler MyXThread in debug mode... Is there a way to keep the worker thread busy while waiting for http job response?
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
You are right. I though also of two ways of doing this after I posted it. The idea was to serve a resource demanding process from another webapp backend. So in the other backend if all requests were coming all together it would be a disaster...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…