python: concurrent programming (twenty-six)

foreword

This article will discuss with you the actual project of python concurrent programming: win graphical interface application (eight articles 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 application of the scene.

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

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

The address of the next article is as follows:

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

1. Package the script into a win application

To package the Python applications in this chapter into executable files that can run on various Windows platforms, tools such as PyInstaller, cx_Freeze, or PyOxidizer can be used. These tools can package Python code and dependent libraries into independent executable files that can be run without installing a Python interpreter.

The following uses PyInstaller as an example to demonstrate how to package the above Python application into a Windows executable file:

(1) Install PyInstaller:

pip install pyinstaller

 (2) Switch to the directory where the application is located, and execute the following command:

pyinstaller --onefile your_script.py

 This will generate an your_script.exeexecutable named . --onefileThe parameter means to generate a single executable file. If you want to generate a folder containing multiple files, you can remove this parameter.

(3) After completion, an executable file will be generated in the dist directory.

Please note that because the packaging tool will package the application's dependent libraries and interpreter together, the generated executable file may be relatively large. In addition, some specific Python libraries may have compatibility issues when they are packaged, requiring additional configuration or processing.

During the packaging process, you can configure the parameters of PyInstaller, such as specifying the icon of the application, adding additional files, etc. For specific configuration options, please refer to the documentation of PyInstaller.

Also, in addition to PyInstaller, you can also try other packaging tools, such as cx_Freeze and PyOxidizer, etc., which provide similar functionality. You can choose the right tool according to your needs.

Note that it is best to package your application on the target platform to ensure that the resulting executable runs well on that platform. If you need to support multiple platforms, you can perform packaging operations on each platform separately.

Finally, it is recommended to test the application carefully before packaging to ensure that everything works correctly in the packaged executable. The following is the situation of running the exe file after the packaging is successful:

 2. Summary of concurrent programming

 1. The benefits of concurrent programming for win applications

(1) Improve performance: Concurrent programming can take advantage of multi-core and multi-thread to execute multiple tasks at the same time, thereby improving the performance and response speed of the application. Computation-intensive or I/O-intensive tasks can be handled more efficiently by making full use of system resources.

(2) Increase scalability: Through concurrent programming, applications can handle more concurrent requests and improve the scalability of the system. The multi-thread or multi-process design can support processing multiple client requests at the same time and improve the throughput of the system.

(3) Improve user experience: Concurrent programming can make applications more flexible and responsive. By placing time-consuming tasks in a background thread or process, you can avoid blocking the user interface, maintain the smoothness of the application, and provide a better user experience.

(4) Improve code structure: Concurrent programming can help decompose complex tasks into multiple independent concurrent units, making the code more modular and maintainable. By using concurrency mechanisms such as threads, processes, or coroutines, tasks can be assigned to different execution units, and coordinated and communicated through appropriate synchronization mechanisms.

(5) Implement concurrent design patterns: Concurrent programming provides various concurrent design patterns and technologies, such as producer-consumer pattern, thread pool, asynchronous programming, etc., which can simplify complex concurrent operations and provide a consistent programming model. These patterns and techniques improve code readability, maintainability, and reuse.

Overall, concurrent programming enables developers to take full advantage of the multi-core and multi-thread capabilities of modern computer systems, increasing the performance and responsiveness of applications while providing a better user experience and scalability. However, concurrent programming also needs to pay attention to issues such as concurrent security, thread synchronization, and resource management to ensure the correctness and stability of the program.

2. Comparison of learning difficulty of processes, threads, and coroutines

Generally speaking, from the perspective of learning difficulty, process > thread > coroutine:

(1) Process (Process): In concurrent programming, process is the most complicated concept. Creating and managing processes requires system calls provided by the operating system, involving complex mechanisms such as inter-process communication (IPC) and process synchronization. There are independent memory spaces between processes, and data transfer and sharing need to be realized through IPC. It is relatively complicated to write correct inter-process communication code.

(2) Thread (Thread): Compared with the process, the concept and use of the thread are simpler. A thread is an execution unit within a process. Multiple threads share the memory space of a process, allowing for more convenient sharing of data and communication. However, concurrent access to shared data among threads may cause concurrency safety issues, such as race conditions and deadlocks. Writing correct thread-safe code requires consideration of the use of synchronization mechanisms and reasonable thread scheduling.

(3) Coroutine: Coroutine is a lightweight user-level thread, and its use is simpler than processes and threads. The switching overhead of coroutines is small and can support a large number of concurrent tasks. However, coroutines require specific programming language support or use a specific coroutine framework. The writing of coroutines usually requires the help of concepts such as asynchronous programming model and non-blocking IO, which may have a certain learning curve for beginners.

It should be noted that although coroutines are relatively simple in some specific scenarios, for complex concurrency issues and advanced applications, it is still necessary to have a deep understanding of the concepts, models and mechanisms of concurrent programming, as well as the consideration of concurrency security and performance. Therefore, although it is relatively easy to get started with coroutines, it still requires certain experience and skills in practice.

3. Which is more commonly used in concurrent programming of processes, threads, and coroutines?

In Python, most modules naturally support threaded concurrent programming. Thread is a common concurrent programming method. Python provides a built-in thread module threading, which makes thread creation and management relatively easy.

However, fewer modules directly support concurrent programming with coroutines than concurrent programming with threads. Coroutine is a lightweight concurrent programming method, which can be implemented through asynchronous programming frameworks or libraries, such as asyncio, , curioand trioso on. These frameworks provide coroutine creation, scheduling and management mechanisms, making coroutine programming more convenient and efficient.

For process concurrent programming, although Python provides multiprocessingmodules for creating and managing processes, not all modules directly support process-level concurrent operations. It is necessary to combine multi-process programming techniques and tools according to specific modules and requirements to realize concurrent operations between processes.

To sum up, most modules naturally support thread concurrent programming, relatively few modules directly support coroutine concurrent programming, and for process concurrent programming, it needs to be selected and adapted according to the specific situation.

4. The same function, multi-process, multi-thread, multi-coroutine, which one has less code

In general, the amount of code for multi-threading is relatively small, while the amount of code for multi-process and multi-coroutine may be more.

The amount of multi-threaded code is relatively small because the resources of the process are shared between threads, and no additional communication and synchronization mechanisms are required. Threads can directly access shared data and resources, so when writing multithreaded code, you usually only need to pay attention to basic operations such as thread creation, start, and end, as well as access and protection of shared resources.

In contrast, the amount of code for multi-process and multi-coroutine may be more. Multi-process programming needs to deal with communication and synchronization between processes, and usually requires the use of inter-process queues, pipes, shared memory and other mechanisms to exchange and share data. These additional communication and synchronization mechanisms increase the complexity and volume of the code.

Multi-coroutine programming also needs to deal with communication and synchronization between coroutines, and usually requires the use of specific asynchronous programming frameworks or libraries to manage and schedule coroutines. In coroutine programming, you also need to pay attention to details such as event loop, coroutine switching and waiting, and communication between coroutines. These additional mechanisms and details also increase the complexity and volume of the code.

Overall, the amount of code for multithreading is relatively small, while the amount of code for multiprocessing and multicoroutines may be more, because they need to deal with additional communication, synchronization and scheduling mechanisms. However, the specific code size will still be affected by specific problems and implementation methods. Therefore, when choosing a concurrent programming method, factors such as code size, performance, and maintainability need to be considered comprehensively.

Guess you like

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