Search code examples
multithreadingdelphihttpindy

Delphi/Indy IdHttpServer not multithreaded?


I'm using Delphi 2006 and Indy 10. I create a form and drop down an IdHttpServer component. I make an OnCreate event for the form to set the server active, and I enter these lines for the server's OnCommandGet:

procedure TForm3.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
   Beep;
   Sleep(10000);
   AResponseInfo.ContentText := DateTimeToStr(Now);
end;

Note the Sleep for 10 seconds.

I then test with Firefox, using 2 browsers. I have the first one connect to "localhost", and I hear a beep right away. I then tab to the 2nd browser, and have it connect to localhost (in less than 10 seconds), but it doesn't beep right away. It waits for the 1st request to complete, then beeps, and waits another 10 seconds.

I thought these components were multi-threaded? Is there some property I can set to make it behave the way I thought it would (both requests would get answered immediately).


Solution

  • Not Indy and the TIdHTTPServer is responsible for this behaviour! It's the webbrowser!

    Firefox shares the TCP connection for different requests at the same server.

    So, Firefox serializes the 2 requests for the same URI. Open 2 different browsers at the same time (e.g. IE and Firefox), request http://localhost/ in both and you will get the expected result.

    And the answer to your question: Yes, of course, every TIdHTTPServer.OnCommandGet event is executed in an own "scheduler" thread, and can be executed simultaneously.