为什么在返回`NotReady`后没有重复调用`Future :: poll`?(Why is `Future::poll` not called repeatedly after returning `NotReady`?)
请考虑以下代码
extern crate futures; use std::sync::{atomic, Arc}; use futures::*; struct F(Arc<atomic::AtomicBool>); impl Future for F { type Item = (); type Error = (); fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> { println!("Check if flag is set"); if self.0.load(atomic::Ordering::Relaxed) { Ok(Async::Ready(())) } else { Ok(Async::NotReady) } } } fn main() { let flag = Arc::new(atomic::AtomicBool::new(false)); let future = F(flag.clone()); ::std::thread::spawn(move || { ::std::thread::sleep_ms(10); println!("set flag"); flag.store(true, atomic::Ordering::Relaxed); }); // ::std::thread::sleep_ms(20); let result = future.wait(); println!("result: {:?}", result); }
生成的线程设置了一个未来等待的标志。 我们也睡眠了生成的线程,因此
.poll()
的初始.poll()
调用是在设置标志之前。 这导致.wait()
)无限地阻塞(看似)。 如果我们取消注释另一个thread::sleep_ms
,.wait()
返回,并打印出结果(()
)。我希望当前的线程尝试通过多次调用
poll
来解决未来,因为我们阻止了当前的线程。 但是,这种情况并没有发生。我试图阅读一些文档 ,似乎问题是线程在第一次从
poll
获取NotReady
后NotReady
了。 但是,我不清楚为什么会这样,或者如何解决这个问题。我错过了什么?
Consider the following code
extern crate futures; use std::sync::{atomic, Arc}; use futures::*; struct F(Arc<atomic::AtomicBool>); impl Future for F { type Item = (); type Error = (); fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> { println!("Check if flag is set"); if self.0.load(atomic::Ordering::Relaxed) { Ok(Async::Ready(())) } else { Ok(Async::NotReady) } } } fn main() { let flag = Arc::new(atomic::AtomicBool::new(false)); let future = F(flag.clone()); ::std::thread::spawn(move || { ::std::thread::sleep_ms(10); println!("set flag"); flag.store(true, atomic::Ordering::Relaxed); }); // ::std::thread::sleep_ms(20); let result = future.wait(); println!("result: {:?}", result); }
The spawned thread sets a flag, which the future waits for. We also sleep the spawned thread, so the initial
.poll()
call from.wait()
is before the flag is set. This causes.wait()
to block (seemingly) indefinitely. If we uncomment the otherthread::sleep_ms
,.wait()
returns, and prints out the result (()
).I would expect the current thread to try to resolve the future by calling
poll
multiple times, since we're blocking the current thread. However, this is not happening.I have tried to read some docs, and it seems like the problem is that the thread is
park
ed after gettingNotReady
from thepoll
the first time. However, it is not clear to me why this is, or how it is possible to work around this.What am I missing?
原文:https://stackoverflow.com/questions/43503953
最满意答案
你可以这样做:
var CountOfitem= (from FI in context.FactorItems join P in context.tblparts on FI.PartRef equals P.PartsID where (FI.FactorRef == FactorID) select new { gridcount=FI.Rowno }).Count();
获得物品数量。
You could do:
var CountOfitem= (from FI in context.FactorItems join P in context.tblparts on FI.PartRef equals P.PartsID where (FI.FactorRef == FactorID) select new { gridcount=FI.Rowno }).Count();
to get the number of items.
相关问答
更多-
MVC实体框架计数(MVC Entity Framework Count)[2022-09-27]
不确定你是如何匹配的,但也许是这样的 from c in aCategories where c.Search.Any(s => s.SearchStandard.Contains("Aston Martin") == true) select new SearchPreviewListCategoriesViewModel { CategoryID = c.Id, Name = c.Name, SearchPreviewControllerAction = c.SearchPreviewControlle ... -
查询语法 var query = from p in context.People group p by p.name into g select new { name = g.Key, count = g.Count() }; 方法语法 var query = context.People .GroupBy(p => ...
-
使用实体框架代码时如何忽略属性[复制](How to ignore a property when using Entity Framework Code First [duplicate])[2023-07-16]
将[System.ComponentModel.DataAnnotations.Schema.NotMapped]属性添加到该属性。 Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property. -
从实体框架获取计数(Get Count from entity framework)[2022-02-15]
我实际上使用了这个名为Linqer的工具,因为我有SQL,我只是将它弹出到该工具中,它为我生成了Linq。 这是出来的: var SKUStock = (from sli in entities.StockLineItems where sli.SystemArea.SystemAreaID == 1 && sli.Wareho ... -
计入实体框架(Counting in Entity Framework)[2021-10-07]
抱歉打扰你,刚刚找到解决方案! 与.Distinct()。 ObjGetIndexedViewResult.TotRecord = listDocuments.Distinct().AsNoTracking().Select(x => new { x.id }).Count(); sorry for bothering ya, just found the solution! with .Distinct(). ObjGetIndexedViewResult.TotRecord = listD ... -
如果基础类型是ICollection
, Enumerable.Count ( IEnumerable 的扩展方法)只调用Count ,因此对于List 没有区别。 Queryable.Count ( IQueryable 的扩展方法)将使用底层查询提供程序,这在很多情况下会将计数降低到实际的SQL,这将比计算内存中的对象更快。 如果应用了过滤器(例如Count(i => i.Name = "John") ),或者基础类型不是ICollection ,则枚举该集合以计算计 ... -
实体框架6多对多(Entity Framework 6 Many to many)[2023-10-18]
你永远不会将OrderItems添加到DbContext集合中。 你的代码也有问题,这意味着你多次覆盖你的项目和变量值。 下面的代码做了一些假设,因为你没有提供更多的代码,但应该很容易适应。 using (var context = new EFRecipesContext()) { var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) }; context.Orders.Add(order); ... -
你可以这样做: var CountOfitem= (from FI in context.FactorItems join P in context.tblparts on FI.PartRef equals P.PartsID where (FI.FactorRef == FactorID) select new { ...
-
直接使用Entity Framework是不可能的。 我认为这非常适合您已有的专用UserRepository类。 作为旁注,我会尽量避免对每个用户进行单独的数据库调用,而是可以使用单个查询解决此问题,例如[警告:未经测试的代码]: var users = GetUsersFromActiveDirectory(); // get the nof contacts per user fill in the contacts count for each user // assuming a IsC ...
-
看看这两个链接,它们可能有助于解决您的问题: 如何在不加载内容的情况下COUNT在EntityFramework中的行? http://msdn.microsoft.com/en-us/data/jj574232.aspx Why? Because it is not made for this purpose. And how can I change this behavior? You really can't - only if you start to mess around with the E ...