标签: deferred-execution

为什么不能将延迟传递给Python Twisted中的回调?

d = Deferred()
d.callback(Deferred()) # Assertion error saying that a Deferred shouldn't be passed
Run Code Online (Sandbox Code Playgroud)

为什么是这样?我查看了代码并提交了messages/Trac,并且没有理由说明为什么会出现这种情况.绕过这个最明显的方法就是把它Deferred放在一个元组中,但为什么这个限制在这里呢?

python twisted deferred-execution

2
推荐指数
1
解决办法
303
查看次数

如何在调试时评估延迟的Linq语句?

我在VS2010中进行调试,我想检查一个字符串值,但我可以通过手表,悬停,本地等方式向调试器显示:

"System.Linq.Enumerable+<TakeIterator>d__3a`1[System.Char]"
Run Code Online (Sandbox Code Playgroud)

我不在乎是否存在过早评估或其他任何副作用,我只是想看看如果我现在在当前断点处评估表达式将会评估什么.

这是怎么做到的?我也可以通过以前评估的方式更改我的代码吗?当我没有调试时,我并不在乎......但只是想知道.

如果它是相关的...(我怀疑它.)我在将一个新的实体对象保存到数据库之前填充...一些字段分配了LINQ语句,我不确定它们何时被评估为EF的封面.数据库更新失败,'字符串或二进制数据将被截断...所以我试图找到太长的字段.

c# linq debugging visual-studio-2010 deferred-execution

2
推荐指数
1
解决办法
1006
查看次数

使用返回IEnumerable

我遇到的有趣问题是完全有道理的.我有一个像这样的通用方法:

public TResult Run<TResult>(Func<SqlDataReader, TResult> resultDelegate)
{
   TResult result;

   using (SqlDataReader reader = command.ExecuteReader()) // command is SqlCommand with attached SqlConnection
   {
      result = resultsDelegate(reader);
   }

   // Some other unrelated code (but that's why result has a variable)

   return result;
}
Run Code Online (Sandbox Code Playgroud)

在一种情况下,resultDelegate返回类型(TResult)是IEnumerable<object>.问题是Run由于延迟执行,函数立即返回,处理SqlDataReader.稍后在代码中,当我尝试读取结果时(代表所做的reader.Read(),我得到了一个InvalidOperationException: Invalid attempt to call Read when reader is closed.

我很难找到解决这个问题的最佳方法.我知道我可以返回一个具体的清单,但如果可能的话我想避免这样做.我也可以在委托中移动using语句,但是再一次,如果我可以避免为每个委托做这个,那就太好了.有任何想法吗?

.net delegates using-statement deferred-execution

2
推荐指数
1
解决办法
279
查看次数

这两个linq实现有什么区别?

我正在经历Jon Skeet的Reimplemnting Linq to Objects系列.在where文章的实现中,我发现了以下片段,但是我没有得到通过将原始方法分成两部分来获得的优势.

原始方法:

// Naive validation - broken! 
public static IEnumerable<TSource> Where<TSource>( 
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate) 
{ 
    if (source == null) 
    { 
        throw new ArgumentNullException("source"); 
    } 
    if (predicate == null) 
    { 
        throw new ArgumentNullException("predicate"); 
    } 
    foreach (TSource item in source) 
    { 
        if (predicate(item)) 
        { 
            yield return item; 
        } 
    } 
}
Run Code Online (Sandbox Code Playgroud)

重构方法:

public static IEnumerable<TSource> Where<TSource>( 
    this IEnumerable<TSource> source, 
    Func<TSource, bool> predicate) 
{ 
    if (source == null) 
    { 
        throw new …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-objects where deferred-execution

2
推荐指数
1
解决办法
117
查看次数

Rails 3 - 如何推迟解析JavaScript

我正在尝试优化我的网站,我得到的一个建议是推迟解析JavaScript.我用谷歌搜索了几个小时,但我没有遇到一个优雅的解决方案,在Rails 3中这样做.我正在使用标准

<%= javascript_include_tag :application -%>
Run Code Online (Sandbox Code Playgroud)

在我的代码中标记,就在之前.有人知道一种相对简单的方法来推迟加载application.js文件的所有Javascript吗?谢谢.

javascript deferred-execution ruby-on-rails-3

2
推荐指数
1
解决办法
1215
查看次数

通过使用带有async/await的Linq延迟执行来阻止它

我是async/await的新手,并且正在修改它以使用任务列表对对象列表执行操作.我使用Linq生成对象列表和任务列表.下面的示例看起来有点人为,但它是我实际代码的简化版本.

我发现当代码如图所示执行时,在所有任务完成之后(等待之后),对象的Now属性都没有更新,并且所有任务的状态仍为Running.

我发现通过.ToList <>()将对象和任务转换为实际列表来消除Linq延迟执行,我的代码按预期工作(填充对象,任务全部运行完成).

我熟悉Linq延迟执行,但我真的很困惑这个代码中发生了什么(不是).我可能在async/await中犯了一个noob错误......它是什么?

private class Foo {
    public DateTime Now { get; set; }
}

private void Button_Click( object sender, EventArgs e ) {
    PopulateDates();
}

private async void PopulateDates() {
    var ordinals = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, };

    var foos = ordinals.Select( o => new Foo() ); //.ToList();

    var tasks = foos.Select( f => PopulateDateAsync( f ) ); //.ToList();

    await Task.WhenAll( tasks );

    var firstNow = foos.ElementAt( 0 …
Run Code Online (Sandbox Code Playgroud)

c# linq deferred-execution async-await

2
推荐指数
1
解决办法
1456
查看次数

IEnumerable <T>有结果直到Count()或Any()被调用

我正在测试Linq扩展方法的性能变化,我遇到了一个奇怪的情况.

当执行返回测试时,首先调用Count()将返回1,后续的Any()为false.

首先调用Any()时,它为true,后续Count()为0.

在调用任一方法之前的断点检查显示有1个项目符合预期,但枚举在以这种方式枚举后或通过调用Any()或Count()后为空.

有人可以解释这种行为吗?由于延迟执行的一些警告,我的实现中是否存在错误?

public class Thing
{
    public Guid Id { get; set; }
}

[TestClass]
public class IEnumerableExtensionsTests
{
    Guid[] thingKeys = new Guid[1] { Guid.Parse("11A1AA1A-1A11-A111-AA11-111111AA1A11") };
    System.Collections.ObjectModel.Collection<Thing> things= new System.Collections.ObjectModel.Collection<Thing>();
    int additionalThingCount = 100;

    [TestMethod]
    public void TestIntersect1()
    {
        DateTime start = DateTime.Now;
        var exceptionsList = things.Intersect1(thingKeys, (e) => e.Id);
        //int count1 = exceptionsList.Count();
        //Assert.AreEqual<int>(thingKeys.Length, count1);
        bool any1 = exceptionsList.Any();
        int count2 = exceptionsList.Count();
        bool any2 = exceptionsList.Any();
        string key = thingKeys[0].ToString();
        var first = exceptionsList.FirstOrDefault(); …
Run Code Online (Sandbox Code Playgroud)

c# linq ienumerable deferred-execution

2
推荐指数
1
解决办法
162
查看次数

了解C#中的延迟加载优化

在阅读了一些如何使用,foreach,linq延迟执行和迭代器在C#中工作之后.我决定尝试在一个小项目中优化基于属性的验证机制.结果:

private IEnumerable<string> GetPropertyErrors(PropertyInfo property)
{
    // where Entity is the current object instance
    string propertyValue = property.GetValue(Entity)?.ToString();

    foreach (var attribute in property.GetCustomAttributes().OfType<ValidationAttribute>())
    {
        if (!attribute.IsValid(propertyValue))
        {
            yield return $"Error: {property.Name} {attribute.ErrorMessage}";
        }
    }
}

// inside another method
foreach(string error in GetPropertyErrors(property))
{
    // Some display/insert log operation
}
Run Code Online (Sandbox Code Playgroud)

我觉得这很慢,但也可能是由于反射或大量的属性要处理.

所以我的问题是...... 这是最优还是很好地利用了延迟加载机制?或者我错过了什么,只是浪费了大量的资源.

注意:代码意图本身并不重要,我关心的是在其中使用延迟加载.

c# linq lazy-loading deferred-execution

2
推荐指数
1
解决办法
668
查看次数

产量返回IEnumerable <IEnumerable <... >>

以下代码List<string>在yield返回之前创建一个中间实例并向其追加值.有没有一种好方法可以避免创建实例并且直接返回单元格值?

IEnumerable<IEnumerable<string>> GetStrValues()
{
    ......
        foreach (var r in rows)
        {
            var row = new List<string>();
            foreach (var c in r.Cells())
            {
                var value = getCellStr(c);
                row.Add(value);
            }
            yield return row;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# linq deferred-execution

2
推荐指数
1
解决办法
312
查看次数

为什么在c#中使用"yield"关键字进行延迟执行会有不同的运行时行为?

如果IgnoreNullItems在下面的sammple代码中调用扩展方法,则延迟执行按预期工作,但是在使用时IgnoreNullItemsHavingDifferentBehaviour会立即引发异常.为什么?

List<string> testList = null;
testList.IgnoreNullItems(); //nothing happens as expected

testList.IgnoreNullItems().FirstOrDefault();
//raises ArgumentNullException as expected

testList.IgnoreNullItemsHavingDifferentBehaviour(); 
//raises ArgumentNullException immediately. not expected behaviour -> 
//  why is deferred execution not working here?
Run Code Online (Sandbox Code Playgroud)

感谢您分享您的想法!

Raffael Zaghet

public static class EnumerableOfTExtension
{
    public static IEnumerable<T> IgnoreNullItems<T>(this IEnumerable<T> source)
        where T: class
    {
        if (source == null) throw new ArgumentNullException("source");

        foreach (var item in source)
        {
            if (item != null)
            {
                yield return item;
            }
        }
        yield break;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

.net c# yield keyword deferred-execution

1
推荐指数
1
解决办法
322
查看次数