python: concurrent programming (twenty-five)

foreword

This article will discuss with you the actual project of python concurrent programming: win graphical interface application (part seven, eight in total) , the series of articles will build the project from scratch, gradually improve the project, and finally make the project suitable for high concurrency scene application.

This article is the twenty-fifth article of python concurrent programming. The address of the previous article is as follows:

Python: Concurrent Programming (24)_Lion King's Blog-CSDN Blog

The address of the next article is as follows:

Python: Concurrent Programming (26)_Lion King's Blog-CSDN Blog

1. Concurrency testing

1. Observe the number of pycharm threads (rough observation)

(1) When the test protocol client is not running, the number of threads is 70

(2) The number of threads running the client is 79

(3) Open the client, the number of threads is 74

(4) Increase the connection, the number of threads is 77

 (5) When transferring data, the number of threads is 75

(6) Establish connection again and send data, the number of threads is 73

(7) Increase the serial number to 10 and the number of threads to 72

 (8) Why does the number of threads mentioned above not change linearly?

Some factors that can affect the variation in the number of threads include:

①Thread pool mechanism of Python interpreter: Python interpreter may use thread pool to manage threads, so existing threads may be reused when executing thread tasks instead of creating new threads every time. This increases the efficiency of threads, but may result in the number of threads not increasing strictly linearly.

②Thread scheduling of the operating system: The operating system is responsible for the scheduling and management of threads, and it may optimize and adjust the creation and destruction of threads. The operating system may combine or reuse some thread resources, or dynamically adjust the size of the thread pool according to the actual situation, so as to improve the overall performance of the system.

③The impact of other threads or processes: There may be other threads or processes running in the system at the same time, and they will also occupy certain resources. The presence of these threads or processes may affect the change in the number of threads so that it is not strictly linear.

Therefore, in your test, although you have established multiple TCP connections, the change in the number of threads may be affected by the above factors, resulting in results that are not strictly linear. This is normal and nothing to worry about. The most important thing is to ensure that your application has the expected function and performance at runtime.

2. Accurate observation of the number of threads

(1) Increase the number of threads to print at the place where the data is received

print(threading.active_count())

 (2) Observed Quantitative Changes

① Establish TCP communication and accept data. At this time, the number of threads is 2, which should be the main thread + 1 TCP sub-thread

②Every time a tab page is added, a new sub-thread is added, which shows a linear increase, which is also in line with the code logic

These data show that our application has the capability of multi-threaded concurrency. At present, the actual test supports at least 10 clients with high concurrency, which can meet my current needs.

2. Test summary

1. About application functions

The application has many bugs and unfriendly interactions. For example, even if the tab page is closed, the TCP connection cannot be closed, which may affect debugging. The function is actually only TCPclient. The details are as follows:

(1) Tab function

Create TCP Client Tab: By clicking on the "TCP Client" menu item, a TCP Client tab can be created in the application for connecting to the server and sending/receiving messages.

(2) In the TCP client tab, the specific functions include:

Enter the server IP address and port number.

Click the "Connect" button to connect to the specified server.

Enter a message in the send message input box, and click the "Send" button to send the message to the server.

Displays received server messages in the received messages area.

(3) Other functions include:

Support closing the tab, that is, when closing the connected TCP client tab, the connection with the server will be disconnected.

2. About thread monitoring

In Python, you can use the following methods to accurately monitor threads:

(1) Using threadingmodules: threadingModules are built-in modules for multi-threaded programming in Python. By using Threadclasses to create thread objects, you can monitor the status and execution of threads. You can use is_alive()the method to check whether the thread is alive, and join()the method to wait for the thread to finish executing.

(2) Using threading.active_count()functions: threadingThe module provides active_count()functions to obtain the number of currently active threads. This function can be called periodically to monitor changes in the number of threads.

(3) Using threading.enumerate()functions: The functions threadingof the module enumerate()can return the list of currently active thread objects. By comparing the results of the two calls before and after, you can determine whether the thread has increased or decreased.

(4) Use threading._activedictionary: threadingThe module maintains a _activedictionary inside, which records the currently active thread objects and their identifiers. The number of active threads can be obtained by checking the length of the dictionary.

(5) Use the hook function: You can use the custom hook function to make a callback when the thread is created and ended, and record the status and execution of the thread in the callback function.

(6) Use third-party tools or libraries: You can also use third-party tools or libraries to monitor threads. For example, the psutillibrary can obtain system thread information, and threading_exthe library provides more thread monitoring functions, such as thread execution time, thread lock, etc.

These methods can choose an appropriate way to monitor the status and number of threads according to the requirements. Note that in multi-threaded programming, due to the concurrent execution between threads, thread safety issues may be encountered. Therefore, thread synchronization and lock mechanisms need to be paid attention to when monitoring threads to ensure the accuracy of monitoring data.


 

Guess you like

Origin blog.csdn.net/weixin_43431593/article/details/131364629