8.3 use the binary data record layout
The Struct module provides the pack () and unpack () methods to process binary formats with variable lengths. The following example shows how to use the title information loop in a zipfile that does not use the zipfile module. The compression codes "H" and "I" represent two and four unsigned numbers, respectively, and "<" indicate that they are both standard sizes and sorted by little-endian bytes.
8.4 Multithreading
A thread is a technique for separating disconsecutive and dependent tasks. The thread can improve the response of programs that allow user input, while other programs are running in the background. A related application is to execute operations in another thread while running I/O.
The following code shows that the main program continues to run when the high-priority threading module executes tasks in the background.
The biggest challenge for multi-threaded applications is to coordinate the threads that share data or resources. In the end, the thread module provides a large number of synchronization primitives, including locks, time, condition variables, and semaphores.
Despite the powerful functions of these tools, minor design errors may lead to irreparable failures. Therefore, the first choice for task coordination is to centralize all access to resources in a single thread, and then use the queue module to serve requests from other threads. Applications that use Queue objects for internal communication and coordination are easier to design, read, and reliable.