Delphi/Indy IdHttpServer 不是多线程的?

发布于 2024-07-16 16:57:30 字数 618 浏览 2 评论 0原文

我正在使用 Delphi 2006 和 Indy 10。我创建一个表单并下拉一个 IdHttpServer 组件。 我为表单创建了一个 OnCreate 事件以将服务器设置为活动状态,并为服务器的 OnCommandGet 输入以下行:

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

注意睡眠 10 秒。

然后我使用 Firefox 进行测试,使用 2 个浏览器。 我有第一个连接到“localhost”,我立即听到一声蜂鸣声。 然后我点击第二个浏览器,并让它连接到本地主机(不到 10 秒),但它不会立即发出蜂鸣声。 它等待第一个请求完成,然后发出蜂鸣声,然后再等待 10 秒。

我认为这些组件是多线程的? 是否有一些我可以设置的属性,使其按照我想象的方式运行(两个请求都会立即得到答复)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁以何悠 2024-07-23 16:57:30

不是 Indy,而是 TIdHTTPServer 造成了这种行为! 这是网络浏览器!

Firefox 在同一服务器上为不同请求共享 TCP 连接。

因此,Firefox 会序列化对同一 URI 的 2 个请求。 同时打开2个不同的浏览器(例如IE和Firefox),在两者中请求http://localhost/,您将得到预期的结果。

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

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.

万水千山粽是情ミ 2024-07-23 16:57:30

我使用过 Indy 10 idHTTPServer,它是多线程的。 阻止您的应用程序的可能是“嘟嘟声”或“睡眠”命令。 因为虽然组件是多线程的,但是有些命令仍然可能会锁定整个进程。

I have used Indy 10 idHTTPServer and it is multithread. 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.

牵你的手,一向走下去 2024-07-23 16:57:30

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;

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文