Fire multiple streams instantly at akka.net

I'm not sure if this applies more to akka.net or TPL, but I will use the actors for my example to clarify the issue.

The question in a nutshell: is there a way to tell akka.net to start more threads simultaneously than I have a kernel kernel? Here's an example of code and details:

I am currently using a laptop with an 8-core processor. So let me say that I create 20 actors:

for (int i = 0; i < 20; i++) { actorList.Add(system.ActorOf<ExampleActor>()); } 

And then I pass the message to all those:

 actorList[0].Tell(new ExampleMessage()); Console.WriteLine("sent message to actor 1\t" + DateTime.Now.TimeOfDay); actorList[1].Tell(new ExampleMessage()); Console.WriteLine("sent message to actor 2\t" + DateTime.Now.TimeOfDay); ... actorList[19].Tell(new ExampleMessage()); Console.WriteLine("sent message to actor 20\t" + DateTime.Now.TimeOfDay); 

All the actor does when receiving a message is the following fake lengthy process:

 Console.WriteLine("Accessing slow service\t" + DateTime.Now.TimeOfDay + "\t" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep(60000); Console.WriteLine("Service call was successful\t" + DateTime.Now.TimeOfDay + "\t" + Thread.CurrentThread.ManagedThreadId); 

I also have this code in its constructor, so I know when the actor is actually created:

 public ExampleActor() { Console.WriteLine("Creating " + this.Self.Path + DateTime.Now.TimeOfDay); .... } 

So, now when I launch the application, I first get the line "sent message to actor" for all 20 participants - in the same millisecond. But then, when the time comes from initialization, what happens is that at first only 8 actors are created.

Then, since none of the other participants completed the task, EXACTLY after 1 second, another one is initialized (9th). In another second, our 10th actor appeared and launched the Thread.Sleep operation.

Now the question is: is there a way to tell akka.net (or TPL below) that I am sure that the threads will wait ~ 60 seconds for each service call? So that all 20 threads were started immediately, instead of immediately starting 8, and all 20 threads started only after 12 seconds passed?

+5
source share
1 answer

Akka actors work on .Net ThreadPool by default.

You have a ThreadPool puzzle where all available threads are busy (one per processor). When all its threads are busy, ThreadPool adds another thread per second to its pool.

To "solve" your problem, you can increase the minimum number of threads with ThreadPool.SetMinThreads. But the real solution is to not block threads (rather than block participants).

If your workload is tied to the processor, this will not go faster if more participants are involved at the same time.

If your workload is related to IO, you should call it asynchronously:

 Receive<string>(msg => { DoSomethingAsync(msg).PipeTo(Self); } Receive<Result>(msg => { // Process the result } 
+3
source

Source: https://habr.com/ru/post/1243621/


All Articles