sogou / workflow

C++ Parallel Computing and Asynchronous Networking Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

server端如何使用WFGraphNode

chanchann opened this issue · comments

当我们服务器端有比较复杂的逻辑时,想用DAG构建业务逻辑

正确打开方式是怎样的呢

下面是错误代码

    WFHttpServer server([](WFHttpTask *server_task) { 
        WFGraphTask *graph = WFTaskFactory::create_graph_task(nullptr);
        WFGraphNode& server_node = graph->create_graph_node(server_task);
        
        auto http_task_01 = WFTaskFactory::create_http_task("www.baidu.com", 4, 2, http_callback);
        auto http_task_02 = WFTaskFactory::create_http_task("www.bing.com", 4, 2, http_callback);
        WFGraphNode& http_node_01 = graph->create_graph_node(http_task_01);
        WFGraphNode& http_node_02 = graph->create_graph_node(http_task_02);
        server_node-->http_node_01;
        server_node-->http_node_02;
    });

你这个想法很有意思!但是这个代码不可行。不可以用server task来产生一个graph node。
正确的方式是:

    WFHttpServer server([](WFHttpTask *server_task) { 
        WFGraphTask *graph = WFTaskFactory::create_graph_task(nullptr);

        auto http_task_01 = WFTaskFactory::create_http_task("www.baidu.com", 4, 2, http_callback);
        auto http_task_02 = WFTaskFactory::create_http_task("www.bing.com", 4, 2, http_callback);

        WFGraphNode& http_node_01 = graph->create_graph_node(http_task_01);
        WFGraphNode& http_node_02 = graph->create_graph_node(http_task_02);

        http_node_01-->http_node_02;

        series_of(server_task)->push_back(graph);
    });

graph是一种任务,可以添加到server task所在的series。以上代码先抓取baidu.com,再抓取bing.com,之后回复请求。如果去掉'http_node_01-->http_node_02;'这句,则相当于图里没有任何边,两个抓取并行执行,都结束之后回复请求。

。◕◡◕。)ノ 感谢指导