我的異步呼叫似乎在串行執行中運行..讓我相信我在代碼中沒有正確地做某事,或者..也許不理解某些東西
在較高的級別上,我接收 100,000 個左右的字串,使用它們執行限制為每個連接每秒 1 個的查詢速率。我從 4 個服務連接開始,并希望同時使用所有 4 個 .. 等待他們的所有連接答案回來然后結合結果
主檔案
struct DataFetcher {
DataFetcher(cpool::ConnectionPool& tws_conn_pool): conn_pool(&conn_pool){
std::cout << "DataFetcher() this->conn_pool == "<< this->conn_pool << std::endl;
}
std::vector<std::string> process_data(std::vector<std::string> sub_batch_vector){
std::for_each(sub_batch_vector.begin(), sub_batch_vector.end(),[](std::string query_str){
std::cout << query_str << ";";
std::this_thread::sleep_for (std::chrono::seconds(1));
});
cpool::ConnectionPool::ConnectionProxy proxy_conn = this->my_conn_pool->get_connection();
proxy_conn->is_healthy();
// will do more stuff later here
return sub_batch_vector;
}
cpool::ConnectionPool * const tws_conn_pool;
};
在回圈中使用此異步呼叫的正確方法是什么.. 將有可變數量的 ServiceConnections 可用,所以我想做這樣的事情
std::vector<std::future<std::vector<std::string>>> results_vector;
for (int i=0 ; i < this->tws_conn_pool->size()-1; i ) {
std::vector<std::string> subvector = {queries_list.begin() (batch_size*i), queries_list.begin() (batch_size*(i 1))};
DataFetcher fetcher = DataFetcher(*this->conn_pool);
auto fut = std::async(std::launch::async, &DataFetcher::process_data, &fetcher, subvector);
results_vector.push_back(fut);
}
但這似乎很鈍,實際上并沒有編譯..
from /Server/cpp_server/src/_server.cpp:1:
/usr/include/c /9/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::future<std::vector<std::__cxx11::basic_string<char> > >; _Args = {const std::future<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&}; _Tp = std::future<std::vector<std::__cxx11::basic_string<char> > >]’:
/usr/include/c /9/bits/alloc_traits.h:482:2: required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::future<std::vector<std::__cxx11::basic_string<char> > >; _Args = {const std::future<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&}; _Tp = std::future<std::vector<std::__cxx11::basic_string<char> > >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::future<std::vector<std::__cxx11::basic_string<char> > > >]’
/usr/include/c /9/bits/stl_vector.h:1189:30: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::future<std::vector<std::__cxx11::basic_string<char> > >; _Alloc = std::allocator<std::future<std::vector<std::__cxx11::basic_string<char> > > >; std::vector<_Tp, _Alloc>::value_type = std::future<std::vector<std::__cxx11::basic_string<char> > >]’
/Server/cpp_server/src/_server.cpp:175:35: required from here
/usr/include/c /9/ext/new_allocator.h:145:20: error: use of deleted function ‘std::future<_Res>::future(const std::future<_Res>&) [with _Res = std::vector<std::__cxx11::basic_string<char> >]’
145 | noexcept(noexcept(::new((void *)__p)
| ^~~~~~~~~~~~~~~~~~
146 | _Up(std::forward<_Args>(__args)...)))
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Server/cpp_server/src/_server.cpp:4:
/usr/include/c /9/future:782:7: note: declared here
782 | future(const future&) = delete;
| ^~~~~~
make[2]: *** [CMakeFiles/_server.dir/build.make:63: CMakeFiles/_server.dir/src/_server.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/_server.dir/all] Error 2
uj5u.com熱心網友回復:
讓資料獲取器通過指標獲取其池。參考參考并立即獲取其地址是愚蠢的。
將 移動fut到向量中。期貨不能復制,只能移動。
按值傳遞fetcher給異步。對區域變數的指標/參考不應傳遞給異步。
移入. subvector_ async效率。
銷毀時異步任務上的異步塊產生的未來(或移動到目的地)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425964.html
上一篇:原始指標但現代方式
