ca9163d9
ca9163d9

Reputation: 29209

Is there a more F# idiomatic way for code using TPL?

I'm converting the following C# code to F#. (https://github.com/confluentinc/confluent-kafka-dotnet/blob/master/examples/AvroGeneric/Program.cs)

var consumeTask = Task.Factory.StartNew(() =>
{
    while (true)
    {
        consumer.Poll(100);
    }
});

consumeTask.Wait();

Should it be replaced by Async workflow? BTW, is it a way not to use Poll?

Upvotes: 2

Views: 279

Answers (1)

Aaron M. Eshbach
Aaron M. Eshbach

Reputation: 6510

For a more F#-idiomatic way of using Kafka in general, take a look at the Kafunk library from Jet.com. It has a nice F# API wrapping the Confluent .NET Kafka library, and in my experience it's also quite performant.

For TPL Tasks, you can use F# Async Workflows in much the same way:

let poller = 
    async {
        while true do
            consumer.Poll 100
    }

poller |> Async.Start

If you want to use a Task inside your Async Workflow, you can use Async.AwaitTak, or you can blend them together using a custom workflow, like this one.

Upvotes: 3

Related Questions