2
How do I make a pool
of Thread
, I need to execute a process that contains several records, but I need to send on demand, send 10 and as you release, send more.... how can I do ?
I set an example...
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
MinhaThread = class(TThread)
procedure Execute; override;
procedure Verifica;
procedure Fechar;
Private
constructor Create();
end;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
thread: MinhaThread;
public
{ Public declarations }
procedure consultaProcesso(Sender: TObject);
procedure postJSON(JSON:String);
end;
var
Form1: TForm1;
I : Integer;
JSON:String;
implementation
{$R *.dfm}
{ MinhaThread }
constructor MinhaThread.Create;
begin
inherited
Create(True);
FreeOnTerminate := True;
Priority := tpLower;
Resume;
end;
procedure MinhaThread.Execute;
Var Sender : TObject;
begin
Synchronize(Verifica);
Form1.consultaProcesso(Sender); // Executar Rotina ( Procedures )
while not Terminated do
begin
Sleep (10);
Terminate; // Finaliza a Thread
Synchronize(Fechar);
end;
end;
procedure MinhaThread.Fechar;
begin
//application.terminate;
end;
procedure MinhaThread.Verifica;
begin
Form1.Caption := 'EXECUTANDO...'+IntToStr(I);
end;
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
thread := MinhaThread.Create();
end;
procedure TForm1.consultaProcesso(Sender: TObject);
begin
//exemplo com o for
// porem aqui eu percorro a query, passo
// para a variavel JSON o json que está na query
// e chamo o metodo post
for I := 0 to 100 do
begin
postJSON(JSON);
end;
end;
procedure TForm1.postJSON(JSON: String);
begin
//faz um post pelo idHTTP;
Memo1.Lines.Add(DateTimeToStr(now)+ ' - Executando JSON '+IntToStr(I));
//para simular um tempo de espera do retorno
Sleep(1000);
//retorno...
Memo1.Lines.Add(DateTimeToStr(now)+ ' - Retorno JSON '+IntToStr(I));
end;
end.
In the method consultaProcesso
put a for
, but it would be the same as the while
that I do in the query
catching the JSON
and moving on to the way postJSON
, in this method put a sleep
to simulate a return time I have from post
carried out by idHTTP
.
Could you post what you already have? so it would be easier for us to help.
– Roberto de Campos
I have a query with about 10,000 records, what I do is go through it and do a post with idHttp; .... basically; while not qryRegistros.eof of Begin //monto json with the records //I call the post postJSON(JSON); qryRegistros.next; end;
– Robinho de Morais
Ok, if you limit the amount of records returned by your query, it does not solve the problem?
– Roberto de Campos
I guess not, since I have to generate everyone’s json and send it, then the plan would be to put it in a thread pool to go processing....
– Robinho de Morais
Right, but with each Thread run you want to run 10 Query records, correct?
– Roberto de Campos
would send 10 records to 10 threads, wait and as soon as you finish sending the next....
– Robinho de Morais
I get it, give me the code of this your query, so I use it myself to do the listing.
– Roberto de Campos
It’s simple, I have the field with json, so I just pass it in the post.... <code> with query.eof do Begin postJSON(query.Fieldbyname('JSON'). asString)); query.next; end; </code>
– Robinho de Morais
Which version of Delphi are you using?
– Roberto de Campos
use the Delphi Berlin
– Robinho de Morais
Since you are passing a very superficial structure, I will only be able to help you superficially too, within Form1 you have the thread variable, this variable would be the one I put in my code Minhasthreads: Array of Minhathread;. and your query should be within Form1 and for every 10 record you would create a new thread within the list, as I wrote in my reply below. Then just wait for all threads to finish running.
– Roberto de Campos
as the original code queries the bank and uses other classes, I made this functional example that approaches 100% of the process and problem I have, anyway I appreciate the attention and I will try here, thank you.
– Robinho de Morais
Take a look at http://www.omnithreadlibrary.com/index.htm. is a framework that does thread Poll and more! Download and look at the example 11_ThreadPool
– MarcosK