Forum > Networking and Web Programming

Application.Threaded in web applications

(1/2) > >>

egsuh:
Hi,

Following is the program file when I create a new FastCGI project.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program fcgiproject1; {$mode objfpc}{$H+} uses  fpFCGI, unit1; begin  Application.Title:='fcgiproject1';  { Uncomment the port setting here if you want to run the     FastCGI application stand-alone (e.g. for NGINX) }  // Application.Port:=2015; // For example  Application.Initialize;  Application.Run;end.
And following is the program file when I create a new HTTP server application, with threaded on.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program httpproject1; {$mode objfpc}{$H+} uses  fphttpapp, unit1; begin  Application.Title:='httpproject1';  Application.Port:=8080;  Application.Threaded:=True;  Application.Initialize;  Application.Run;end.                                      

My question is, is  "threaded" not necessary/meaningful for FastCGI applications?  I'd like to run FCGI application on NGINX.


         

zeljko:
I don't know too much about fcgi and it's threaded abilites, but if you run it on unix likes eg linux then you should include cthreads (if you plan to use threads in your fcgi application).

Warfley:
Depends, do you want to be able to handle more than one request concurrently? If so you should use threaded.

It's a different story if you are compiling a (non fast) CGI application, where for each request a new process is started, which makes threading redundant.
I would actually recommend using CGI over fast CGI, because spawning processes on Linux systems is about as efficient as spawning a thread (in fact, for the linux kernel a thread and a process are basically the same thing). Also FPC applications are pretty lightweight, as they usually do not rely on many external dependencies (a pure fpc application does not even bind to LibC).

FastCGI was developed for scripting languages like PHP, because starting a new PHP process requries loading the complete PHP interpreter into memory, which is slow. You don't have that issue with native FPC, applications, so just go for CGI. It's the easiest way to implement a web application.

So my recommendation is, go for CGI and you don't need to care about threading

egsuh:

--- Quote ---I would actually recommend using CGI over fast CGI, because spawning processes on Linux systems is about as efficient as spawning a thread (in fact, for the linux kernel a thread and a process are basically the same thing). Also FPC applications are pretty lightweight, as they usually do not rely on many external dependencies (a pure fpc application does not even bind to LibC).

--- End quote ---

Thank you very much for your comment. Currently I'm running CGI on Windows, but thinking moving to FCGI for following reasons. Actually my application does not need threaded in FCGI mode, as CGI is serving rather good. Just checking any difference between HTTP server and FCGI.

But regarding CGI and FCGI, I think as follows. Please let me know your idea on them, if any.

1) The difference between CGI and FCGI is not just between spawning a process or a thread. CGI must be loaded from HDD (or SSD) at every request, while FCGI remains in memory. Won't this cause rather large difference?

2) My webserver needs connection to Firebird database. DB connection takes long compared with other operations. So I'd like to keep DB connected even during idle time. This can be done only with FCGI.

What do you think?

Warfley:

--- Quote from: egsuh on January 27, 2023, 10:22:04 am ---1) The difference between CGI and FCGI is not just between spawning a process or a thread. CGI must be loaded from HDD (or SSD) at every request, while FCGI remains in memory. Won't this cause rather large difference?

--- End quote ---
Your OS will cache frequently used files, meaning this should not be an issue. That said, spawning a process under Windows is quite slow (because each process gets a window assigned, which takes much more effort than under linux), so using threads for a windows host is actually much faster than starting a process (which is not the case on Linux)


--- Quote from: egsuh on January 27, 2023, 10:22:04 am ---2) My webserver needs connection to Firebird database. DB connection takes long compared with other operations. So I'd like to keep DB connected even during idle time. This can be done only with FCGI.

--- End quote ---
Database connections are (usually) not thread safe, so either you need to create access control through critical sections, which introduce a great overhead and might force serialize your theads, removing basically any advantage of threading, or you need to establish one connection per thread anyway.

Also don't pre-maturely optimize something that does not need to be optimized. A database connection will be established once every request, an a request, even with a very good network connection will take around 10-20 milliseconds just network travel time. If the database is running on the same network as the host, the connection will probably be established in less than 1 millisecond. Does it really matter if the user waits 20ms vs 21 ms for a request to finish?

Navigation

[0] Message Index

[#] Next page

Go to full version