简体   繁体   English

Delphi / Indy IdHttpServer没有多线程?

[英]Delphi/Indy IdHttpServer not multithreaded?

I'm using Delphi 2006 and Indy 10. I create a form and drop down an IdHttpServer component. 我正在使用Delphi 2006和Indy 10.我创建了一个表单并下载了一个IdHttpServer组件。 I make an OnCreate event for the form to set the server active, and I enter these lines for the server's OnCommandGet: 我为表单设置OnCreate事件以将服务器设置为活动状态,然后为服务器的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. 注意睡眠10秒钟。

I then test with Firefox, using 2 browsers. 然后我用Firefox测试,使用2个浏览器。 I have the first one connect to "localhost", and I hear a beep right away. 我有第一个连接到“localhost”,我立刻听到一声哔哔声。 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. 然后我选择第二个浏览器,并将其连接到localhost(在不到10秒的时间内),但它不会立即发出哔声。 It waits for the 1st request to complete, then beeps, and waits another 10 seconds. 它等待第一个请求完成,然后发出蜂鸣声,并等待另一个10秒。

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). 是否有一些我可以设置的属性使其表现得像我想象的那样(两个请求都会得到立即回答)。

Not Indy and the TIdHTTPServer is responsible for this behaviour! Not Indy和TIdHTTPServer负责此行为! It's the webbrowser! 这是webbrowser!

Firefox shares the TCP connection for different requests at the same server. Firefox在同一服务器上共享不同请求的TCP连接。

So, Firefox serializes the 2 requests for the same URI. 因此,Firefox序列化了对同一URI的2个请求。 Open 2 different browsers at the same time (eg IE and Firefox), request http://localhost/ in both and you will get the expected result. 同时打开2个不同的浏览器(例如IE和Firefox),在两者中请求http://localhost/ ,您将获得预期的结果。

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. 您的问题的答案:是的,当然,每个TIdHTTPServer.OnCommandGet事件都在自己的“调度程序”线程中执行,并且可以同时执行。

GUI is responsive during this 10 seconds, so it is multiThreaded for long operations put your code into another thread - and you will get what you want GUI在这10秒内响应,所以它是多线程的长时间操作将你的代码放入另一个线程 - 你会得到你想要的

unit uSomeThread;

interface

uses
  System.Classes;

type
  TSomeThread = class(TThread)
  protected
    procedure Execute; override;
  end;

implementation


procedure TSomeThread.Execute;
begin
  //  Beep;
  Sleep(10000);
end;

end.

........... ...........

    procedure TServer.IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  cContext: TClientContext;
  t: TSomeThread;
begin
//  Beep;
//  Sleep(10000);

  t := TSomeThread.Create(true);
  t.FreeOnTerminate := true;
  t.Start;

  AResponseInfo.ResponseNo := 200;
  AResponseInfo.CacheControl := 'no-cache';
  AResponseInfo.CustomHeaders.Add('Access-Control-Allow-Origin: *');
  AResponseInfo.ContentText := 'ok';
  AResponseInfo.ResponseNo := 200;
  AResponseInfo.WriteContent;
  Beep;

end;

I have used Indy 10 idHTTPServer and it is multithread. 我使用过Indy 10 idHTTPServer,它多线程的。 What blocking your app might be the "beep" or the "sleep" command. 什么阻止你的应用程序可能是“哔”或“睡眠”命令。 Because although the component is multithread, some commands may still lock the whole process. 因为虽然组件是多线程的,但某些命令仍可能锁定整个过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM