future 相关问题

计算结束前计算结果的占位符。用于并发编程。关于未来事件的问题在Stack Overflow上是偏离主题的。

如何在等待未来完成时避免 while 循环?

我对 java future 和处理函数有疑问。代码示例: 公共 HealthCheckResponse 调用(){ String redisHost = this.configuration.getRedisHost(); log.info(“连接到r...

回答 1 投票 0

如何停止输入冲突或过期的约会?

当前时间是在代码开始时使用...定义的 从日期时间导入日期时间 现在 = 日期时间.now() auformat = now.strftime("%d-%m-%Y %H:%M:%S") # 打印(自动格式) 我需要停止同意...

回答 1 投票 0

如何禁止同时输入和过去的预约?

当前时间是在代码开始时使用...定义的 从日期时间导入日期时间 现在 = 日期时间.now() auformat = now.strftime("%d-%m-%Y %H:%M:%S") # 打印(自动格式) 我需要停止同意...

回答 1 投票 0

如何停止同时输入和过去的预约?

当前时间是在代码开始时使用...定义的 从日期时间导入日期时间 现在 = 日期时间.now() auformat = now.strftime("%d-%m-%Y %H:%M:%S") # 打印(自动格式) 我需要停止同意...

回答 1 投票 0

Java 异步代码中的屈服控制

我有 Rust/C#/Javascript 异步编程背景,并试图理解类似的 Java 习惯用法。具体来说,我想了解是否存在“绿色线程&

回答 2 投票 0

如何取消Future.wait(在dart中)?

我有两个异步运行的 Future 函数: 列表> 工作 = []; jobs.add(asyncFunction1()); jobs.add(asyncFunction2()); jobsResults = 等待 Future.wait(jobs); 如果

回答 1 投票 0

在按钮单击 Flutter 时调用异步方法

我有例如这个未来的功能: Future sayHello() 异步 { 等待服务.sayHello(); } 我想在单击按钮时调用它 升高的按钮( onPressed: () => sayHel...

回答 3 投票 0

未来的软件包说 mcapply 已知不稳定是什么意思?

我试图从未来的包装中理解下面的报价。 “已知不稳定”是什么意思?这是否意味着 mcapply 可能会崩溃,或者是 mcapply 不可靠的结果...

回答 1 投票 0

从 R 兑现的承诺中获取价值

我读了很多关于 R Promise 的文章(包括这篇),但仍然不明白。 参见代码: 图书馆(未来) 图书馆(承诺) 计划(多进程) 读取.csv.async <- function(file, header = TRUE,

回答 4 投票 0

R future:显示进度+等待完成

我正在使用 R 包 future 来运行大型计算作业。我想在三个核心之间并行化工作,并为每个核心都有一个单独的进度条。我也想等一下剧本

回答 1 投票 0

Scala 等待 Future.sequence 的 Await.result,哪些 future 没有按时完成?

我有期货清单并等待 val listWithResult = Await.result(Future.sequence(listOfFutures), 10.分钟) 它失败了 java.util.concurrent.TimeoutException:期货在 [10 分钟后超时...

回答 1 投票 0

如果没有人持有对 CompletableFuture 的引用,它是否总是会在被垃圾收集之前完成?

Java 对象一旦无法访问就有资格进行垃圾回收,我的困惑是无法访问的 CompletableFutures 会发生什么,如下所示: 无效一些方法(){ methodThatProduceCompletableFut...

回答 1 投票 0

Scala 用于理解响应 Future.filter 谓词不满足

我得到了以下代码 val obj1Seq: Future[Seq[Object1]] = /* 调用函数返回 Future[Seq[Object1]] */ val obj2Seq: Future[Seq[Object2]] = /* 调用函数返回 Future[Seq[Object2]] */...

回答 1 投票 0

当我不等待 Future 时,如何处理从 Futures 返回的错误?

我有这样的事情: 未来 f1() { ... } 未来 f2() { ... } 未来 f3() { ... } void main() 异步 { f1(); f2(); // 可能会抛出异常 等待...

回答 1 投票 0

使用 Kafka 进行模拟单元测试 - 回调模拟生产者

我想围绕调用 Kafka 设置一些测试。我的 Kafka 调用有以下回调设置,请参见下文: @Async // 允许函数异步。还必须设置配置 @欧维...

回答 1 投票 0

将值从 Future 映射到对象而不是 Future[Object]

我有一个返回 Future[State] 的函数和另一个我需要返回 StateSimplified 对象的函数 //类状态 案例类状态(id:字符串,标签:字符串,邮政编码:字符串,扩展名:

回答 1 投票 0

在 Flutter 的 FutureBuilder 中检查 `snapshot.hasData` 和 `snapshot.data != null` 是否多余?

我正在使用 Flutter 的 FutureBuilder,并发现了一种模式,在渲染小部件之前会检查 snapshot.hasData 和 snapshot.data != null: 未来建造者( 未来:未来, 布...

回答 2 投票 0

clojure 未来与延迟

我一直在阅读《Programming in Clojure》并发现了文本 (defn 获取文档 [id] ; ...做一些工作来检索已识别文档的元数据... {:url "http://www.mozilla....

回答 2 投票 0

库币期货Python API,Kline获取数据不起作用

我正在尝试获取比特币期货市场(符号为“.KXBTUSDT”)特定时间(2022 年 4 月 4 日)的 Klines 历史数据。然而,当我调用该函数时,API 返回...

回答 3 投票 0

使用多个线程改变结果

我正在尝试利用多线程来优化 C++ 代码的运行时间。我尝试了几种不同的解决方案。 我有这个使用 boost 的代码: #包括 #包括 我正在尝试利用多线程来优化 C++ 代码的运行时间。我尝试了几种不同的解决方案。 我有这个使用 boost 的代码: #include <iostream> #include <vector> #include <chrono> #include <atomic> #include <thread> #include <boost/asio.hpp> #include <boost/bind/bind.hpp> void Test::boost_worker_task() { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved_counter; } void Test::run(const unsigned int games_to_solve, const bool use_mul_thread) { MyEngine::init_rand(); const auto start = std::chrono::high_resolution_clock::now(); const unsigned int num_threads = use_mul_thread ? std::thread::hardware_concurrency() : 1; std::cout << "Using " << num_threads << " threads to solve " << games_to_solve << " games" << std::endl; boost::asio::io_service io_service; boost::asio::thread_pool pool(num_threads); for (unsigned int i = 0; i < games_to_solve; ++i) { io_service.post([] { return Test::boost_worker_task(); }); } // Run and wait for all tasks to complete io_service.run(); pool.join(); const auto end = std::chrono::high_resolution_clock::now(); const std::chrono::duration<double, std::milli> elapsed = end - start; std::cout << "Solved " << games_solved_counter << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed.count() / 1000.0 << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed.count() << " milliseconds\n" << std::endl; } 还有这段代码: #include <iostream> #include <vector> #include <thread> #include <mutex> #include <chrono> #include <atomic> #include <future> std::atomic<int> games_solved(0); void TestMulThread::worker_task(const unsigned int num_iterations, std::mutex& games_solved_mutex) { for (unsigned int i = 0; i < num_iterations; ++i) { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved; } } void TestMulThread::run(const unsigned int total_games_to_solve) { MyEngine::init_rand(); const auto start_time = std::chrono::high_resolution_clock::now(); const unsigned int num_threads = std::thread::hardware_concurrency(); const unsigned int games_per_thread = total_games_to_solve / num_threads; const unsigned int remaining_games = total_games_to_solve % games_per_thread; std::cout << "Using " << num_threads << " threads to solve " << total_games_to_solve << " games" << std::endl; // Distribute the remaining games std::vector<unsigned int> games_for_each_thread(num_threads, games_per_thread); for (unsigned int i = 0; i < remaining_games; ++i) { games_for_each_thread[i]++; } std::vector<std::future<void>> futures; std::mutex games_solved_mutex; for (unsigned int i = 0; i < num_threads; ++i) { futures.push_back(std::async(std::launch::async, worker_task, games_for_each_thread[i], std::ref(games_solved_mutex))); } for (auto& future : futures) { future.get(); } const auto end_time = std::chrono::high_resolution_clock::now(); const auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count(); std::cout << "Solved " << games_solved << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed_time / 1000.0 << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed_time << " milliseconds\n" << std::endl; } 我的问题有两个: boost 版本的运行速度比第二个代码慢得多。它甚至比使用简单的 for 循环并且不尝试利用多个线程运行得更慢。我知道尝试并行运行任务可能会导致不同类型的开销,但第二个代码运行速度非常快,我想了解原因。 使用 Visual Studio 时,第二个代码片段运行得非常快(使用 Release 和 -O2 标志进行编译作为 C++ 优化),但是当我使用 g++ 在我的 Linux 机器上编译并运行相同的代码时,它的运行速度再次比使用 for 慢。循环运行相同数量的游戏。我尝试使用一些不同的设置进行编译,例如: g++ -O2 -o test Test.cpp -std=c++20 -lpthread g++ -O2 -o test Test.cpp -std=c++20 -pthread 对于为什么会出现这种情况有什么想法吗? 谢谢! 第一个程序从不使用线程池。除了加入。 您不知道/显示 hardware_concurrency 的值,因此您无法知道您正在将苹果与苹果进行比较。 如果至少修复第一个,则两者之间的性能匹配: 住在Coliru 文件test.h #pragma once #include <atomic> namespace Test { static void boost_worker_task(); static void run(unsigned, bool); static inline std::atomic_uint games_solved_counter{0}; }; // namespace Test namespace TestMulThread { static void worker_task(unsigned); static void run(unsigned); static inline std::atomic_uint games_solved{0}; }; // namespace TestMulThread struct MyEngine { using State = char[3][3]; static void init_rand(); static void random_start_state(State&); static void solve_game(State&); }; 文件test.cpp #include "test.h" #include <boost/asio.hpp> #include <iostream> #include <random> #include <thread> using namespace std::chrono_literals; static constexpr auto now = std::chrono::high_resolution_clock::now; void MyEngine::init_rand() {} void MyEngine::random_start_state(State&) {} void MyEngine::solve_game(State&) { thread_local static std::mt19937 prng{std::random_device{}()}; thread_local static std::uniform_int_distribution work(3, 8); std::this_thread::sleep_for(1ms * work(prng)); }; void TestMulThread::worker_task(unsigned num_iterations) { MyEngine::init_rand(); for (unsigned int i = 0; i < num_iterations; ++i) { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved; } } void TestMulThread::run(unsigned total_games_to_solve) { auto start_time = now(); unsigned num_threads = std::thread::hardware_concurrency(); unsigned games_per_thread = total_games_to_solve / num_threads; unsigned remaining_games = total_games_to_solve % games_per_thread; std::cout << "Using " << num_threads << " threads to solve " << total_games_to_solve << " games" << std::endl; // Distribute the remaining games std::vector games_for_each_thread(num_threads, games_per_thread); for (unsigned int i = 0; i < remaining_games; ++i) { games_for_each_thread[i]++; } std::vector<std::future<void>> futures; for (unsigned i = 0; i < num_threads; ++i) futures.push_back(std::async(std::launch::async, worker_task, games_for_each_thread[i])); for (auto& future : futures) future.get(); auto elapsed_time = now() - start_time; std::cout << "Solved " << games_solved << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed_time / 1.s << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed_time / 1ms << " milliseconds\n" << std::endl; } void Test::boost_worker_task() { char new_state[3][3]; MyEngine::random_start_state(new_state); MyEngine::solve_game(new_state); ++games_solved_counter; } void Test::run(unsigned games_to_solve, bool threaded) { auto const start = now(); unsigned num_threads = threaded ? std::thread::hardware_concurrency() : 1; std::cout << "Using " << num_threads << " threads to solve " << games_to_solve << " games" << std::endl; boost::asio::thread_pool pool(num_threads); for (unsigned i = 0; i < games_to_solve; ++i) post(pool, Test::boost_worker_task); // Run and wait for all tasks to complete pool.join(); auto elapsed = now() - start; std::cout << "Solved " << games_solved_counter << " games!" << std::endl; std::cout << "Elapsed time: " << elapsed / 1.s << " seconds" << std::endl; std::cout << "Elapsed time: " << elapsed / 1ms << " milliseconds\n" << std::endl; } int main() { // auto N = 10'000u; Test::run(N, true); TestMulThread::run(N); } 打印,在线: Using 4 threads to solve 10000 games Solved 10000 games! Elapsed time: 14.04 seconds Elapsed time: 14039 milliseconds Using 4 threads to solve 10000 games Solved 10000 games! Elapsed time: 14.1067 seconds Elapsed time: 14106 milliseconds 对我来说是本地的: Using 8 threads to solve 10000 games Solved 10000 games! Elapsed time: 6.99751 seconds Elapsed time: 6997 milliseconds Using 8 threads to solve 10000 games Solved 10000 games! Elapsed time: 7.03147 seconds Elapsed time: 7031 milliseconds

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.