M
M
Max Maximov2018-04-28 15:55:31
PostgreSQL
Max Maximov, 2018-04-28 15:55:31

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();

Needed: Make as many requests (that is, threads) so that the simultaneous number of connected clients with ongoing transactions is approximately ~ 1000.
Problem: when I run the console with a small number of threads, everything is processed quickly - the database is very powerful, so the result is ~ 1000 connections and processed transactions disappear. And when I set a large number of threads, I get a SocketException - the connection was interrupted by a forcibly remote host. (Reception konskolka complains about the base, and the base complains about the client. Who is right - I don’t understand).
What is my problem? Where to dig? Are there better options for working with threads or options for loading the base as it is written in the condition?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alexs0ff, 2018-04-28
@maximka19

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)

D
Dmitry Donskoy, 2018-04-29
@sentike

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 question

Ask a Question

731 491 924 answers to any question