C
C
client242017-12-02 04:01:46
Python
client24, 2017-12-02 04:01:46

Lots of socket connections. Which is better to use asyncio or threads?

Lots of socket connections. Which is better to use asyncio or threads?
There are many TLS connections
more than 50
, you need to listen to all of them
. What is the best way to implement this?
And what is the best way to use it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2017-12-02
@client24

50 is not much :) A lot is a few thousand. Better use async instead of multithreading. In Python, this is both simpler and more performant in most cases.

V
Vladimir Dubrovin, 2017-12-03
@z3apa3a

When using a single thread with asynchronous processing, only one processor core will be involved. When using a per-request thread, there is a large overhead for creating threads. From here:
1. If each request demands serious processing and loads the processor - use flows.
2. If requests are frequent, but do not require processing - use asynchrony.
3. If you have really high loads and you need to use resources as efficiently as possible - use an application with several pre-launched threads (workers) and use asynchrony in each of them, distribute requests evenly between workers. The number of workers must not be less than the number of cores.
PS When using any approach except for one request - one thread, asynchrony should be not only on sockets, all operations (for example, working with files) should be asynchronous so as not to block the thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question