Python Study Notes (5): Interprocess Communication

Source: Internet
Author: User

Python provides four mechanisms for interprocess communication (IPC): anonymous pipes, named pipes, and sockets) and semaphores (signals ).

Pipeline (pipes) is a cross-regionProgramIs implemented by the operating system. An MPs queue is a one-way channel similar to the shared memory. It provides an interface for collecting simple files at both ends of the MPs queue. Generally, one program writes data to one end of the pipeline, and the other program reads data from the other end of the pipeline. A program can only see the end of its own pipeline and process the data like a common Python file.

The anonymous pipeline (anonymous pipes) usually works with the fork method of the process to associate the parent process with the child process through the shared pipeline file descriptor. The anonymous pipeline also applies to threads. The pipe method of the OS module generates an anonymous pipeline.

The named pipe (Named Pipes) corresponds to the file system of the operating system, represented by a file in the computer. The named pipeline is also called the operating OS. Processes that communicate through a named pipeline can be independent of each other. Operating system. mkfifo to create a named pipe is actually to create an external file, so you need to specify a file path.

Sockets (sockets) is a more common IPC Mechanism than pipelines. Sockets allow different programs on the same computer to transmit data to each other, and allow communications between programs on different computers in the network. When the socket acts as the IPC Mechanism on the same machine, the program connects to the socket through the global port of the machine. When a socket is used for network connection, the program must provide the machine name and port number. The socket module in Python provides socket-related methods, such as bind, listen, accept, Recv, and send.

Semaphores (signals) allow programs to send simple notification events to other programs and trigger corresponding actions. Python provides the signal module to register Python functions as handlers for specific signals.

Python provides the multiprocessing module for sub-process generation and multi-process communication. This module provides process-like process, lock-like lock, IPC tool pipe (anonymous pipeline), shared memory (Value and array), and queue.

 

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.