python: concurrent programming (nineteen)

foreword

This article will discuss with you the actual project of python concurrent programming: win graphical interface application (part 1, a total of eight articles) . 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 nineteenth article of python concurrent programming. The address of the previous article is as follows:

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

The address of the next article is as follows:

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

1. Technology selection

1. Which modules of python support win applications?

In Python, there are several commonly used modules that can be used to design Windows GUI applications. Here is a brief introduction to some of these modules:

(1) Tkinter: Tkinter is Python's standard GUI library, which provides the function of creating various GUI components, including windows, buttons, labels, text boxes, etc. It's simple to use and works on multiple platforms, including Windows.

(2) PyQt: PyQt is the binding of Python's Qt library, which provides a wealth of GUI components and functions. Qt is a cross-platform GUI framework that can be used to develop Windows graphical interface applications. PyQt uses a relatively complex API, but it is very powerful and flexible.

(3) wxPython: wxPython is the binding of Python's wxWidgets library, and it is also a cross-platform GUI framework. It provides many components and tools for creating Windows graphical interfaces, including windows, buttons, menus, etc.

(4) PySide/PySide2: PySide is another binding of Python's Qt library, which provides Qt's GUI functions. It is similar to PyQt, but uses the LGPL licensed version of Qt. PySide can also be used to create Windows GUI applications.

These modules have detailed documentation and sample codes for reference. You can choose suitable modules according to your own needs and preferences to design and develop GUI applications. At the same time, there are many other GUI libraries and tools to choose from, and you can evaluate and make decisions based on the actual situation.

2. What are the advantages of the modules that support win applications?

Know what modules python has, so how should we choose? Let's take a look at their pros and cons:

(1)Tkinter:

Advantage: Tkinter is the standard graphical interface library for Python, so no additional dependencies need to be installed. It is easy to use and suitable for rapid development of simple interface applications.
Disadvantages: Tkinter's default appearance and functions are relatively simple and less customizable.
(2) PyQt:

Advantages: PyQt is a Python binding library based on the Qt framework, which provides rich interface controls and functions, and has good customizability and scalability. It is a mature, stable and widely used graphical interface library.
Cons: PyQt has a steep learning curve, especially for novices. Also, a commercial license for PyQt is available for a fee, but an open source version is also available.
(3) wxPython:

Advantages: wxPython is a Python binding library based on wxWidgets, providing a cross-platform graphical interface development solution. It has good portability, customizability and rich control library, suitable for building complex desktop applications.
Disadvantages: wxPython has relatively few documents, and for some specific functions and customization requirements, more research and practice may be needed.
(4) PySide/PySide2:

Advantages: PySide (PySide2) is also a Python binding library for the Qt framework, similar to PyQt. It provides functions and features similar to PyQt, including powerful customizability and rich control library. Unlike PyQt, PySide uses the LGPL open source license, so it may be more suitable for commercial applications in some cases.
Cons: Due to historical reasons, there are some differences and compatibility issues between PySide and PySide2. At the same time, PySide has relatively little documentation and community support.

These modules provide a wealth of functions and controls, and you can choose the appropriate module to design and develop graphical interface applications according to project requirements and personal preferences. Our company is facing internal customers, and the interface requirements are simple, so Tkinter meets the needs, and we will share based on this in the future.

3. Multi-process, multi-thread or multi-coroutine?

Tkinter-based concurrent programming can use multi-threading.

The Tkinter library itself is single-threaded, meaning it handles interface updates and event responses on the main thread. If time-consuming operations are performed in the main thread, the interface will freeze or become unresponsive, affecting user experience. Therefore, in order to maintain the smoothness and responsiveness of the interface, time-consuming operations can be performed in a background thread, allowing the main thread to focus on interface updating and event processing.

Using multi-threading allows time-consuming operations to be performed concurrently in background threads while keeping the interface responsive on the main thread. Data exchange and synchronization can be performed between threads through an inter-thread communication mechanism (such as a queue). For example, time-consuming data processing tasks can be performed in a background thread, while the main thread is responsible for updating the interface and handling user interaction.

It should be noted that in multi-threaded programming, the issues of thread safety and resource sharing need to be handled well to avoid race conditions and data access conflicts. Thread synchronization mechanisms such as locks and semaphores can be used to ensure thread safety.

In contrast, there are relatively few usage scenarios of multi-process and multi-coroutine in Tkinter applications. Multi-process is suitable for parallel computing involving CPU-intensive tasks, while multi-coroutine is suitable for concurrent processing of I/O-intensive tasks. However, due to the limitations of the Tkinter library itself, the use of multi-process and multi-coroutine may cause problems related to interface interaction and update, so it is generally recommended to use multi-threading to handle concurrent tasks.

Two, realize the function

Our application will be a tool for testing the protocol, such as TCPserver, TCPclient, the interface is also very simple, that is, the computer can be used as the client or server, and multiple tab pages can be opened for protocol testing, and multiple windows are required to be supported.

1. Interface layout display

 2. Performance requirements

Multiple windows are supported, and the number of windows is not limited. Even so, the crash of the host computer should be due to insufficient system resources, and should not depend on the application itself.

Guess you like

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