【发布时间】:2017-01-02 08:35:21
【问题描述】:
我正在尝试在我的测试项目 (How to make a Mutlithreded idhttp calls to do work on a StringList) 中实现流水线模式,但在将 TThread 代码适应流水线模式代码时遇到了困难。关于如何使用它的资源并不多。
我在下面尽力了,请不要投反对票,我知道我的代码很乱,但如果需要我会编辑我的问题。
type
TForm2 = class(TForm)
...
private
procedure Retriever(const input: TOmniValue; var output: TOmniValue);
procedure Inserter(const input, output: IOmniBlockingCollection);
function HttpGet(url: string; var page: string): boolean;
end;
procedure TForm2.startButton1Click(Sender: TObject);
var
pipeline: IOmniPipeline;
i : Integer;
v : TOmniValue;
s : string;
urlList : TStringList;
begin
pipeline := Parallel.Pipeline;
pipeline.Stage(Retriever);
pipeline.Stage(Inserter).NumTasks(10);
pipeline.Run;
for s in urlList do
pipeline.Input.Add(s);
pipeline.Input.CompleteAdding;
// wait for pipeline to complete
pipeline.WaitFor(INFINITE);
end;
function TForm2.HttpGet(url: string; var page: string): boolean;
var
lHTTP: TIdHTTP;
i : integer;
X : Tstrings;
S,M,fPath : String;
begin
lHTTP := TIdHTTP.Create(nil);
X := TStringList.Create;
try
X.Text := lHTTP.Get('https://instagram.com/'+fPath);
S:= ExtractDelimitedString(X.Text);
X.Clear;
Memo2.Lines.Add(fPath+ ' : '+ M ); //how to pass the result to Inserter
finally
lHttp.Free;
end;
end;
procedure TForm2.Inserter(const input, output: IOmniBlockingCollection);
var
result : TOmniValue;
lpage : string;
begin
for result in input do begin
Memo2.Lines.Add(lpage);
FreeAndNil(lpage);
end;
// correect?
end;
procedure TForm2.Retriever(const input: TOmniValue; var output: TOmniValue);
var
pageContents: string;
begin
if HttpGet(input.AsString, pageContents) then
output := //???
end;
【问题讨论】:
-
如果你能真正解释你正在尝试做什么,以及你遇到了什么困难,这会有所帮助。您发布了一堆没有任何解释的代码。
-
@RemyLebeau 我已经在链接的帖子中进行了解释,我想读取 memo1 字符串并将其以管道模式传递给此输入,然后将管道中的结果写入 memo2。
-
OTL 有一个专门用于此目的的队列。
-
@Johan 只是并行可能就足够了。然而,管道理所当然地提供了启动和结束集合,这很方便:-) 是的,在这里使用管道是矫枉过正的,但它也使您免于手动管理集合的创建/销毁
-
@Thunderx
X := TStringList.Create- 你在哪里释放它?如果不需要它,为什么还要创建它?
标签: multithreading http delphi delphi-xe8 omnithreadlibrary