[fpc-pascal] How to use FCGI in threaded mode?

silvioprog silvioprog at gmail.com
Fri Jan 30 00:08:22 CET 2015


Hello,

Using the TCustomHTTPApplication class, I just set the "Threaded" property
to true, and my application works in threaded mode. But, how to do the same
in TCustomFCgiApplication class?

To test it, compile and run the code below. After, open two tabs in your
browser. In tab 1, open the "http://localhost/test?op=loop" URL, in tab 2,
open the "http://localhost/test" URL. Notice that the tab 2 will wait 10
seconds to show the result.

program project1;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}
  CThreads,
{$ENDIF}
  CustWeb, HttpDefs, CustFCGI, Classes, SysUtils;

type

  { TMyFCGIApplication }

  TMyFCGIApplication = class(TCustomFCgiApplication)
  protected
    function InitializeWebHandler: TWebHandler; override;
  end;

  { TMyFCGIHandler }

  TMyFCGIHandler = class(TFCGIHandler)
  public
    procedure HandleRequest(ARequest: TRequest; AResponse: TResponse);
override;
  end;

  { TMyFCGIApplication }

  function TMyFCGIApplication.InitializeWebHandler: TWebHandler;
  begin
    Result := TMyFCGIHandler.Create(Self);
  end;

  { TMyFCGIHandler }

  procedure TMyFCGIHandler.HandleRequest(ARequest: TRequest; AResponse:
TResponse);
  var
    I: Integer;
  begin
    if ARequest.QueryFields.Values['op'] = 'loop' then
    begin
      for I := 1 to 10 do
        Sleep(1000);
      AResponse.Contents.Text := 'Done!';
    end
    else
      AResponse.Contents.Text :=
        'Now: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now);
  end;

begin
  with TMyFCGIApplication.Create(nil) do
    try
      Port := 8080;
      Run;
    finally
      Free;
    end;
end.

Thank you!

-- 
Silvio Cl├ęcio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150129/24e1d608/attachment.html>


More information about the fpc-pascal mailing list