Answer the question
In order to leave comments, you need to log in
How to create >=1000 threads correctly?
Given: there is a PostgreSQL database with 2 stored procedures. There is a C# console with the Npgsql driver. In the console, you need to create 1000 threads, each of which has its own connection and calls these same storages. I call streams and query like this:
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
for (var i = 0; i < 500; i++) {
try {
var thread = new Thread(() =>
{
Console.WriteLine($"Running thread: {Thread.CurrentThread.ManagedThreadId}");
SaveByteA(connectionString);
var a = 2 + 4;
}) {IsBackground = true,Priority = ThreadPriority.AboveNormal};
thread.Start();
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}
Console.WriteLine("Finished Thread #1.");
})
{ IsBackground = true, Priority = ThreadPriority.Highest }.Start();
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
for (var i = 0; i < 500; i++)
{
try {
var thread = new Thread(() =>
{
Console.WriteLine($"Running 2 thread: {Thread.CurrentThread.ManagedThreadId}");
SaveByteB(connectionString);
})
{ IsBackground = true, Priority = ThreadPriority.AboveNormal };
thread.Start();
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}
Console.WriteLine("Finished Thread #2.");
}) { IsBackground = true, Priority = ThreadPriority.Highest}.Start();
Answer the question
In order to leave comments, you need to log in
If your computer is not 1000 core, you do not need CPU bound threads, but IO bound threads. Look for asynchronous methods in your PostgesQl provider (most likely they are prefixed with Sync)
Max Maksimov , see how many simultaneous connections are allowed in the database. If it's low, then increase it.
https://stackoverflow.com/questions/30778015/how-t...
Also read the documentation for the driver: www.npgsql.org/doc/connection-string-parameters.html
You should be interested in Pooling. Try also to increase the number of reserved connections.
Pooling=true;Min Pool Size=0;Max Pool Size=1000;
Pomonitor on what flow there is an exception, learn its index. Add a temporary variable or track it however you like. If it crashes on the same or + -, then most likely see the beginning of the answer).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question