2
\$\begingroup\$

I'm requesting from a web API information about 20,000 people that I need to update continuously at lightning speed (the web server doesn't throttle the number of requests). I can request an update on 20 people at a time (it's the maximum allowed per request) so i loop the List of people and ask the server info on groups of 20 just like this:

Public Sub GetInformation()
    Do
        For Each Person In ListOfPerson
            RequestList.Add Person
            If RequestList.Count = 20 Then RequestToWebServer(RequestList): RequestList.Clear
        Next
    Loop
End Sub

Public RequestToWebServer(ResquestList as List(Of Person))
    'Code to ask the web server the info'
    'Then code to process the response'
    For Each Person In ResquestList
        If Person.Status = "Old" then RequestServerForMoreDetails
    Next
End Sub

This could be very slow because a request could stall and everything just stops waiting for that request to be completed or timed out. So my solution is to fire a thread for each request:

Public Sub GetInformation()
    Do
        For Each Person In ListOfPerson
            RequestList.Add Person
            If RequestList.Count = 20 Then ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf RequestToWebServer), RequestList) : RequestList.Clear
        Next
    Loop
End Sub

I don't know if this is the right way to tackle the problem, worse, I have a second problem. If a Person has a status of "Old" (status is updated when i ask the web API for information) then I have to make an extra web request and I don't want to slow down the process of the others in the list, so I fire them in a thread also like this:

Public RequestToWebServer(ResquestList as List(Of Person))
    'Code to ask the web server the info'
    For Each Person In ResquestList
        If Person.Status = "Old" then ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf RequestServerForMoreDetails), Person)
    Next
End Sub

This creates a lot of threads, and from what I heard it's usually not a good practice. Is there a better way to do this or am I in the right track?

\$\endgroup\$
  • \$\begingroup\$ The tile of the question was changed (the original title was "Should i avoid a high number of threads") and i feel i need to explain that this is a representation of my problem, i'm not dealing with an API about people, it's just an example but that reflects my real problem. My doubt is if i need to use threads to avoid slowing things down. \$\endgroup\$ – user2198816 Nov 23 '16 at 17:30
  • \$\begingroup\$ If it doesn't make a difference in speed to put one or several request to the servers, then why don't you use only one main thread for you app, and a second thread that makes the server requests on the background, and updates as it get results. Doesn't make sense to create thousand of threads if on the other side (the server) it will be answering such requests in a serialized way. \$\endgroup\$ – fernando.reyes Nov 23 '16 at 17:43
  • \$\begingroup\$ The title of the question was changed because, as the title box watermark says, we ask that your title tells us what the code does, not what concerns you have with it. Use the body of the post to express concerns, not the title. Thanks! \$\endgroup\$ – Mathieu Guindon Nov 23 '16 at 17:48
  • \$\begingroup\$ @fernando.reyes Fernando, this server responds in parallel (i think this is the norm on most API's). One request takes about three seconds to download the data. If i, for example, create three threads with one request each i can get all data in about three seconds. If i do it on a single thread it takes me 9 seconds. \$\endgroup\$ – user2198816 Nov 23 '16 at 21:39
  • \$\begingroup\$ @Mat'sMug am i allowed to take this question to stack overflow? maybe it's more appropriate. Thanks for the help. \$\endgroup\$ – user2198816 Nov 24 '16 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.