[SOLVED] HTTPMethod in Synapse is slow and freezes program operation momentarily lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- newman · 2011-01-31 12:40 Hi, I have installed Apache2 server on my computer (OS: Ubuntu 10.04), with PHP and MySqlite support. I am using this function below to send HTTP post method to a PHP file (that resides in a site also in my computer), I am using synapse library. Code: [Select]function PostInetCommand(cPostData:string):string; var   HTTP: THTTPSend;   l: tstringlist;   cResponse:string;   b:boolean; begin   HTTP := THTTPSend.Create;   l := TStringList.Create;   try     if not HTTP.HTTPMethod('POST',cPostData) then       begin         Result:= 'Error';       end     else       begin         l.loadfromstream(Http.Document);         Result:= l.Text;      end;   finally     HTTP.Free;     l.free;   end; end; I find that the program operation freezes momentarily and the response from PHP file is very slow (at least 3 -4 seconds), whereas the same post operation done from a browser is instantaneous. Please explain why this is happening ?? also how can I improve the performance. PS: Sorry if I could not make myself clear as I am new to network programming. thanks nm ---------------------------------------------------------------------- Leledumbo · 2011-01-31 12:43 Mine here is fast, could you make sure that it's faster with php? ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 12:54 Hi newman, Use thread, see demos with threads in: http://code.google.com/p/lazsolutions/ Good luck. ---------------------------------------------------------------------- newman · 2011-01-31 15:28 Quote from: Leledumbo on January 31, 2011, 01:43:38 pmMine here is fast, could you make sure that it's faster with php? I am using PHP..and its not fast... Just for my information how fast should the response from the server be ? Quote from: silvioprog on January 31, 2011, 01:54:47 pmHi newman, Use thread, see demos with threads in: http://code.google.com/p/lazsolutions/ Good luck. thanks for the link, I downloaded the lazsolutions zip file, but threads are a bit steep for me..but I am working on it... ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 15:51 Hi newman, But isn't hardly, it's very easy, please see this: http://code.google.com/p/lazarus- br/source/browse/trunk/misc/BeginThread/MainFrm.pas ---------------------------------------------------------------------- theo · 2011-01-31 16:08 Quote from: silvioprog on January 31, 2011, 04:51:48 pmBut isn't hardly, it's very easy, please see this: How is access to MainForm.DisplayLabel.Caption synchronized in this example? ---------------------------------------------------------------------- Leledumbo · 2011-01-31 16:22 QuoteI am using PHP..and its not fast...Then it's not HTTPMethod problem, it's the processing of POST (in your PHP file) that's slow. QuoteJust for my information how fast should the response from the server be ?Too many things to consider before saying something about this. Nevertheless, at localhost, if the request is simple (i.e. no complicated data processing) it should be in a blink of an eye. ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 16:26 Hi theo, Sorry but I do not understand the question. (My English is not very good) (...) var   MainForm: TMainForm; . . . procedure HeavyProcess; var   I: Integer = 0; begin   while I < 1000 do   begin     MainForm.DisplayLabel.Caption := IntToStr(I); (...) Other situation: http://code.google.com/p/lazsolutions/source/browse/trunk/Demos/LSSMTP Send/Thread/MainFrm.pas#78 ---------------------------------------------------------------------- theo · 2011-01-31 16:59 Quote from: silvioprog on January 31, 2011, 05:26:45 pmHi theo, Sorry but I do not understand the question. (My English is not very good) It looks like this code is updating MainForm.DisplayLabel.Caption from within a separate thread without synchronizing with the main thread.  Afaik this will crash sooner or later. ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 19:58 Hi theo, How can I fix this without using global variable? I have a solution to use in this demo, but using a global variable. ---------------------------------------------------------------------- theo · 2011-01-31 20:46 Quote from: silvioprog on January 31, 2011, 08:58:26 pmHow can I fix this without using global variable? I never use BeginThread(..) and friends, always TThread class. You can see an example here (PT for you  ) http://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial/ pt#A_classe_TThread See: Synchronize(@Showstatus); ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 21:13 Hm..., I always forever use TThread class too, but I avoided because this class is with memory leaks in Lazarus: http://lists.lazarus.freepascal.org/pipermail/lazarus/2010- October/056098.html I will check if this error of memory leak was repaired, if the class is normal in Lazarus now, I come back to use it. Thanks guys. ---------------------------------------------------------------------- theo · 2011-01-31 21:21 I don't think that "bug" was really problematic. But might be fixed in the meantime: http://bugs.freepascal.org/view.php?id=16884 ---------------------------------------------------------------------- theo · 2011-01-31 22:03 Btw. you could also use the Heartbeat feature to avoid "freezing" of your GUI. http://synapse.ararat.cz/doku.php/public:howto:heartbeat ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 22:05 Wow, this bug was solved: http://bugs.freepascal.org/view.php?id=17560 Thanks. ---------------------------------------------------------------------- Silvio Clécio · 2011-01-31 22:37 Well, I solved the problem with thread not safe (using global variable ): http://code.google.com/p/lazarus-br/source/detail?r=156 ---------------------------------------------------------------------- newman · 2011-02-01 08:34 thanks everyone for the insight..I solved the problem by using INet controls. Thanks for the info on threads I intend to use it in my next project..