How to create >=1000 threads?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
13 views
Given: a database on PostgreSQL, which has 2 stored procedures. There are konsolka in C# with the Npgsql driver. In console you need to create 1000 threads, each of which has its connection and causes these same hranilki. Call flow and request the following:
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();

It is necessary: to Make so many requests (i.e. threads) to the number of simultaneous connected clients running transactions was approximately ~1000.
Problem :when I launch the stand with a small number of threads, all handled quickly -- DB is very powerful, so the result in ~1000 connection and process the transaction disappears. And when you put a large number of threads, you get a SocketException-connection interrupted forcibly by remote host. (Reception console complains about the database and the database on the client. who is right-I do not understand).

What my problem is? Where to dig? There are better options of threading or loading of the database as written in the statement?
by | 13 views

3 Answers

0 like 0 dislike
If You have a computer 1000 not nuclear, you need not CPU bound threads, and IO bound threads. Look for asynchronous methods in your PostgesQl provider (most likely they are prefixed Sync)
by
0 like 0 dislike
Similar to the first experience of BTC for a nice surprise to your database. Spread the load so that the number of connections per second does not exceed the credit limit which configured database. And succeed.
by
0 like 0 dislike
MAKS Maksimov, look how many simultaneous connections are allowed from the database. If not enough, increase.
\rhttps://stackoverflow.com/questions/30778015/how-t...

Also read the documentation on the driver: www.npgsql.org/doc/connection-string-parameters.html
You should be interested in Pooling. Try to increase the number of reserved connections.

Pooling=true;Min Pool Size=0;Max Pool Size=1000;


Pomonitor on what thread the exception occurs, find out its index. Add a temporary variable, or whatever track it. If flying on the same or+, is likely to see the beginnings of an answer).
by
110,608 questions
257,186 answers
0 comments
35,533 users