c# - Conditionally blocking IEnumerable<t> yield return result -
i'm hoping can help me find more efficient solution problem below. i'm using ienumerable yield homecoming pattern in multi-threaded pipeline. under conditions working great, however, have scenarios need operations in pipeline occur synchronously instead of in parallel threads right result. (i.e. running concurrency issues).
existing code is:
public override ienumerable<row> execute(ienumerable<row> rows) { foreach (row row in rows) { //do work.... yield homecoming row; } }
i'm thinking of creating optional parameter controls blocking vs. non blocking output of enumerator; along lines of:
public override ienumerable<row> execute(ienumerable<row> rows, bool blockingexecution) { if (blockingexecution) { homecoming blockingexecute(rows); } else { homecoming nonblockingexecute(rows); } } private ienumerable<row> nonblockingexecute(ienumerable<row> rows) { foreach (row row in rows) { //do work.... yield homecoming row; } } private ienumerable<row> blockingexecute(ienumerable<row> rows) { list<row> blockingresult = new list<row>(); foreach(row r in nonblockingexecute(rows)) { blockingresult.add(r); } homecoming blockingresult; }
in blockingexecute function seems inefficient create re-create of ienumerable list forcefulness entire pipeline flush. there improve way this?
in general, "blocking execution" grade of inefficient. i'd utilize toarray
form, , non-blocking version little confusing. supposed work each 1 blocking 1 doesn't?
i this.
public override ienumerable<row> execute(ienumerable<row> rows, bool blockingexecution) { if (blockingexecution) { homecoming rows.toarray(); } else { homecoming nonblockingexecute(rows); // or "return rows". seems best // practice here, in cases anyway // move work elsewhere } } private ienumerable<row> nonblockingexecute(ienumerable<row> rows) { foreach (row row in rows) { //do work.... yield homecoming row; } }
but yes, blocking version require multiple iterations of same list still (one create array, 1 presumably read it). i'm not sure there's way escape that, since want way of loading memory.
c# multithreading ienumerable yield-return
No comments:
Post a Comment