GithubHelp home page GithubHelp logo

AsyncCollection IsEmpty about asyncex HOT 3 OPEN

romerod avatar romerod commented on May 25, 2024
AsyncCollection IsEmpty

from asyncex.

Comments (3)

StephenCleary avatar StephenCleary commented on May 25, 2024 1

Would it be possible to expose the Empty and/or Count property or have a method which gets a specified number of items which are currently available?

No; there's too much potential for misuse; with concurrent/asynchronous collections, any Empty or Count property can be outdated by the time your code even gets the value.

However, there is a better way to meet your need: a Try* method. In the past, I've avoided Try* methods because the meaning of "try" is ambiguous (the most common meaning is "don't throw", but it can also mean "don't behave asynchronously", which would be the meaning in this case). Since System.Threading.Channels has adopted the Try* approach with their high-performance asynchronous collections, I'm comfortable adding Try* methods to mine as well.

from asyncex.

romerod avatar romerod commented on May 25, 2024

Any suggestions on this?

from asyncex.

CZEMacLeod avatar CZEMacLeod commented on May 25, 2024

@romerod @StephenCleary It seems to me that the inner wait loop in DoTakeAsync should probably be guarded on the cancellation token also:

while (Empty && !_completed)

while (Empty && !_completed && !cancellationToken.IsCancellationRequested)
{
  if (sync)
  {
      _completedOrNotEmpty.Wait(cancellationToken);
  }
  else
  {
      await _completedOrNotEmpty.WaitAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
  }
}

I appreciate that as this is async, it might still have the occasional TaskCanceledException if the token was cancelled between the test and the wait, but it would eliminate the problem in this scenario where the token is always cancelled.

I think DoOutputAvailableAsync also needs the change, and then the main loop could be modified to be

var cancelledToken = new CancellationToken(true);

while (true)
{
  IList<Message> buffer = new List<Message>(10000);

  buffer.Add(await asyncQueue.TakeAsync());
  var count = 1;
  while (count < buffer.Count && await asyncQueue.OutputAvailableAsync(cancelledToken))
  {
    try
    {
      // take currently available items
      var message = await asyncQueue.TakeAsync(cancelledToken);
      if (message == null) break;
      buffer[count] = message;
      count++;
    }
    catch (InvalidOperationException)   // Thown if the producer completes
    {
      break;
    }
  }

  await SendMessagesAsync(buffer, count);
}

I think that perhaps the AsyncCollection should have a TryTakeAsync(CancellationToken cancellationToken) method which would be almost the same as the TakeAsync implementation, except not throwing the InvalidOperationException if the underlying take fails.

@romerod As this is open source, you could just include a private copy of the AsyncCollection class, modified for your needs if necessary...

from asyncex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.