N
N
Nikolai2020-12-17 22:34:40
Asynchronous programming
Nikolai, 2020-12-17 22:34:40

What is the optimal structure for a networked multi-threaded python application?

There is a task to write a network application.

Python 3.7 server. Clients access the server via LAN, requests are easy, give data, read data. In addition, the server calculates mathematics based on this data. The calculation of mathematics is started by a timer with a period of about 100 ms. Its calculation time is rather long, up to 100-200 ms.

I think to make two streams - in one mathematics, in another input / output. Use asyncio and threading for this (but this is not accurate). There is no experience in writing such applications and there are several questions)

1. From Google, I realized that the threads actually do not work in parallel, but one at a time. That is, it turns out that a thread with mathematics can block input and output?

2. In general, does the interpreter ever switch threads on its own initiative, or does it always wait until one of the threads stops itself and only then starts another one?

3. can from time to time forcibly pause the thread with mathematics so that the streams with input-output go? and how to do it? sleep()?

4. Or did I generally choose the wrong structure, maybe I need to do it differently?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dr. Bacon, 2020-12-18
@bacon

Let's start with a simple one, asyncio, which runs mattasks via run_in_executor, where the executor is a ProcessPoolExecutor.

D
dooMoob, 2020-12-18
@dooMoob

1) yes
2) the interpreter itself switches threads on a timer
3) it makes no sense, GIL will figure it out on its own
4) mathematics can be moved to a separate process (multiprocessing, celery, etc), but then it will also be necessary to add a communication service between them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question