我寫了一個簡單的程序,可以定期檢查數據。問題在於檢查過程隨着時間的推移而減慢。首先它在一秒鐘內發送2個帖子,在45秒之後發送一個帖子,最終完全停止。我不知道這裏有什麼問題。如何快速分析HTTP Post響應
procedure TForm2.CheckURLs;
var
lHTTP: TIdHTTP;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
N: Integer;
Access: string;
Params, Reply: TStringList;
Cookie: TIdCookie;
begin
lHTTP := TIdHTTP.Create(nil);
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
IdSSL.SSLOptions.Method := sslvTLSv1;
IdSSL.SSLOptions.Mode := sslmClient;
lHTTP.IOHandler := IdSSL;
lHTTP.ReadTimeout := 30000;
lHTTP.HandleRedirects := True;
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.Get('https://www.mywebserver.com', TStream(nil));
Cookie := lHTTP.CookieManager.CookieCollection.Cookie['id', 'www.mywebserver.com'];
if Cookie <> nil then
Access := Cookie.Value;
finally
end;
Params := TStringList.Create;
Reply := TStringList.Create;
TTask.Create(
procedure
var
N, m, i : integer;
begin
Params.Add('IDs=' +B.Strings[i]);
try
lHTTP.Request.CustomHeaders.Values['id'] := Access;
lHTTP.Request.CustomHeaders.Values['X-Requested-With'] := 'XMLHttpRequest';
lHTTP.Request.Referer := 'https://www.mywebserver.com/';
lHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
lHTTP.Request.UserAgent := 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36';
lHTTP.ConnectTimeout := 0;
Reply.Text := lHTTP.Post('https://www.mywebserver.com/', Params);
if AnsiContainsStr(Reply.Text, 'nothing') then
begin
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add(B.Strings[i]);
Label2.Caption := Memo1.Lines.Count.ToString;
end
);
end;
finally
Reply.Clear;
end;
end
).Start;
end;
我測試了RSP-15233,果然只有大約9個任務開始。經過一些測試後,我相信會發生什麼是一個任務被髮送到主線程,並且睡眠呼叫會阻止其他人在至少一段時間內啓動。 添加SyncObject解決了我的問題: x:= 0; 對於i:= 1到25做 TTask.Create(程序開始 的OutputDebugString(PCHAR( '開始::' + TInterlocked.Increment(X)的ToString)); 睡眠(MAXINT); 端)。開始; – FredS
使用了AnonymousThreads,沒有更多的緩慢。 –