future

Scala Option[Future[T]] to Future[Option[T]]

感情迁移 提交于 2020-01-12 04:11:07
问题 How can I convert Option[Future[T]] to Future[Option[T]] in scala? I want to use it in: val customerAddresses = for { a <- addressDAO.insert(ca.address) // Future[Address] ia <- ca.invoiceAddress.map(addressDAO.insert) // Option[Future[Address]] } yield (a, ia) // Invalid value have to be two futures Here signature insert method def insert(address: Address): Future[Address] ca is a CustomerData case class CustomerData(address: Address, invoiceAddress: Option[Address]) 回答1: import scala

Sequential processing of a variable number of async functions in Dart

蹲街弑〆低调 提交于 2020-01-11 11:35:50
问题 I need to repeatedly call an asynchronous function in Dart, let's call it expensiveFunction , for a variable number of arguments. However, since each call is quite memory consuming, I cannot afford to run them in parallel. How do I force them to run in sequence? I have tried this: argList.forEach( await (int arg) async { Completer c = new Completer(); expensiveFunction(arg).then( (result) { // do something with the result c.complete(); }); return c.future; }); but it hasn't had the intended

How to use CompletableFuture.thenComposeAsync()?

最后都变了- 提交于 2020-01-11 05:09:09
问题 Given: public class Test { public static void main(String[] args) { int nThreads = 1; Executor e = Executors.newFixedThreadPool(nThreads); CompletableFuture.runAsync(() -> { System.out.println("Task 1. Thread: " + Thread.currentThread().getId()); }, e).thenComposeAsync((Void unused) -> { return CompletableFuture.runAsync(() -> { System.out.println("Task 2. Thread: " + Thread.currentThread().getId()); }, e); }, e).join(); System.out.println("finished"); } } I am expecting a single executor

Scala Future not entering onComplete()

五迷三道 提交于 2020-01-07 04:20:36
问题 I am trying to grab the value from a snippet of code that results in a Future , but when I run through the code, it turns out that it never enters the onComplete function. Is there anything I'm doing wrong? I've tried using a map as well since it was suggested in other posts, but haven't had success override def findOrCreate(phoneNumber: String, creationReason: String): Future[AvroCustomer] = { //query for customer in db val avroCustomer: Future[AvroCustomer] = customerByPhone(phoneNumber)

Vapor: route return before all modification done

妖精的绣舞 提交于 2020-01-06 07:39:09
问题 I have following route: router.post([Page].self, at: "/fetchStatusOfManagedReleases") { (req, pages) -> Future<[Page]> in let eventIds = pages.map { $0.events }.flatMap { $0 }.map { $0.id } return Release.query(on: req).filter(\.fbId ~~ eventIds).all().map { releases in var result: [Page] = [] for p in pages { let page = p var pageEvents: [Event] = [] for e in p.events { let event = e if let release = releases.first(where: { $0.fbId == e.id }) { event.inProgress = release.inProgress event

Is there a way to launch a tokio::Delay on a new thread to allow the main loop to continue?

霸气de小男生 提交于 2020-01-06 05:50:10
问题 I am trying to run a function at the end of a delay if the timer is not canceled. The use case is a press and hold / double tap for user input. The main issue that I am having is that the tokio::run(task); stops the main loop from executing thus prevents me from evaluating the state of the users control. fn start_timer(&self) { let function_name = self.combo.function_name.clone(); let when = Instant::now() + Duration::from_millis(self.combo.timer_value as u64); let task = Delay::new(when)

How do I erase the type of future in the new future API?

扶醉桌前 提交于 2020-01-06 05:15:17
问题 The following does not compile #![feature(await_macro, async_await, futures_api)] use core::future::Future; async fn foo() {} trait Bar { type Output: Future<Output = ()>; fn bar(&self) -> Self::Output; } impl Bar for () { type Output = Box<dyn Future<Output = ()>>; fn bar(&self) -> Self::Output { Box::new(foo()) } } async fn buz() { await!(().bar()) } error[E0277]: the trait bound `(dyn std::future::Future<Output=()> + 'static): std::marker::Unpin` is not satisfied --> src/lib.rs:19:15 | 19

Empty state page and Navigator issues?

走远了吗. 提交于 2020-01-05 03:55:09
问题 I want to implement empty state page in Flutter app, when news feed is empty. When there is no activity in feed, I want to show ‘no activity’ . When database has data, I want to load this from StreamBuilder to ListView.builder I try implement with: child: StreamBuilder( stream: collection.snapshots(), builder: (context, snapshot) { if(snapshot.hasData) { return ListView.builder( itemBuilder: (context, index) => build(context, snapshot.data.documents[index]), itemCount: snapshot.data.documents

future not working with javax.mail

风流意气都作罢 提交于 2020-01-04 13:40:52
问题 I'm playing with future and and can't seem to get it to work with javax.mail . For example, for fun, I'm trying to set up a compojure handler to grab a bunch of emails and put them into a database, but deliver a response to the client before the emails have all been gathered and inserted. I have a few println s going on in the import-posts function (below), and when I run this from the repl, it works fine, printing 142 journal messages. the first time (because the db is empty), and No new

ProcessPoolExecutor, BrokenProcessPool handling

半腔热情 提交于 2020-01-04 03:19:33
问题 In this documentation ( https://pymotw.com/3/concurrent.futures/ ) it says: "The ProcessPoolExecutor works in the same way as ThreadPoolExecutor, but uses processes instead of threads. This allows CPU-intensive operations to use a separate CPU and not be blocked by the CPython interpreter’s global interpreter lock." This sounds great! It also says: "If something happens to one of the worker processes to cause it to exit unexpectedly, the ProcessPoolExecutor is considered “broken” and will no