Python3.2 official documentation translation-use binary data to record layout and Multithreading

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.