Calling TaskCompletionSource.SetResult in a non blocking manner

I’ve discovered that TaskCompletionSource.SetResult(); invokes the code awaiting the task before returning. In my case that result in a deadlock. Yes, I have a blog post documenting this (AFAIK it’s not documented on MSDN). The deadlock happens because of two things: There’s a mixture of async and blocking code (i.e., an async method is calling … Read more

How to prefetch data using a custom python function in tensorflow

This is a common use case, and most implementations use TensorFlow’s queues to decouple the preprocessing code from the training code. There is a tutorial on how to use queues, but the main steps are as follows: Define a queue, q, that will buffer the preprocessed data. TensorFlow supports the simple tf.FIFOQueue that produces elements … Read more

Wrapping ManualResetEvent as awaitable task

RegisterWaitForSingleObject will combine waits onto dedicated waiter threads, each of which can wait on multiple handles (specifically, 63 of them, which is MAXIMUM_WAIT_OBJECTS minus one for a “control” handle). So you should be able to use something like this (warning: untested): public static class WaitHandleExtensions { public static Task AsTask(this WaitHandle handle) { return AsTask(handle, … Read more

ExecutorCompletionService? Why do need one if we have invokeAll?

Using a ExecutorCompletionService.poll/take, you are receiving the Futures as they finish, in completion order (more or less). Using ExecutorService.invokeAll, you do not have this power; you either block until are all completed, or you specify a timeout after which the incomplete are cancelled. static class SleepingCallable implements Callable<String> { final String name; final long period; … Read more