Output
3.14159262176
Piping pipe
A pipeline is one of the most common methods for UNIX interprocess communication, which uses a read-write channel between parent and child processes for duplex communication. We use Os.read () and Os.write () to read and write the file descriptor, using the Os.close () to close the descriptor.
As a single-process pipeline
Pipeline separated for parent-child process
Output
3.14159262176
Nameless Socket Socketpair
We know that cross-network communication is unavoidable to communicate through sockets, but in this case the multi-process is on the same machine, it is unnecessary to cross the network, using ordinary sockets for communication is a bit wasteful.
Socketpair for a single process
Output
3.14159262176
OS Message Queuing
The operating system also provides a cross-process Message Queuing object that we can use directly, except that Python does not provide a packaged API for direct use by default. We must use third-party extensions to complete the OS Message Queuing communication. Third-party extensions are done by using the C implementation of the Python wrapper.
Output
3.14159262176
SYSTEMV Message Queuing SYSTEMV Message Queuing differs from POSIX Message Queuing. The message queue for SYSTEMV is named as an integer key, and if not specified, it creates a unique, unoccupied integer key. It also provides an integer parameter of the message type, but does not support message precedence.
# coding:utf-8import Osimport sysimport mathimport structimport posix_ipcfrom posix_ipc import Semaphorefrom POSIX_IPC im Port sharedmemory as Memorydef slice (mink, maxk): s = 0.0for k in range (mink, maxk): s + = 1.0/(2*k+1)/(2*k+1) return sdef pi (n):p ids = []unit = N/10sem_lock = Semaphore ("/pi_sem_lock", FLAGS=POSIX_IPC. O_crex, initial_value=1) # Use a semaphore to control multiple processes mutually exclusive access to shared memory ("/PI_RW", Size=8, FLAGS=POSIX_IPC. O_crex) Os.lseek (memory.fd, 0, os. Seek_set) # initialization and double value of 0.0 os.write (MEMORY.FD, Struct.pack (' d ', 0.0)) for I in Range (10): # 10 sub-Processes mink = Unit * Imaxk = Mink + unitpid = os.fork () if PID > 0:pids.append (pid) else:s = Slice (mink, maxk) # child process start Calculation sem_lock.acquire () try:os.ls Eek (MEMORY.FD, 0, os. Seek_set) bs = Os.read (MEMORY.FD, 8) # read out the current value from shared memory cur_val, = Struct.unpack (' d ', BS) # deserialization, comma cannot be less cur_val + = s # plus the results of the current process B s = struct.pack (' d ', Cur_val) # Serialization of Os.lseek (MEMORY.FD, 0, os. Seek_set) Os.write (MEMORY.FD, BS) # Write into Shared memory memory.close_fd () finally:sem_lock.release () sys.exit (0) # The child process ends sums = [] for the PID in Pids:os.waitpid (PID, 0) # waits for the child process to end Os.lseek (MEMORY.FD, 0, os. Seek_set) bs = Os.read (MEMORY.FD, 8) # read out the end of this result sums, = Struct.unpack (' d ', BS) # deserialization MEMORY.CLOSE_FD () # Turn off shared memory Memory.unlink ( # Destroy Shared Memory Sem_lock.unlink () # Destroy Semaphore return Math.sqrt (sums * 8) Print pi (10000000)
Output
3.14159262176
Welcome to my Blog Park and public number: Https://home.cnblogs.com/u/Python1234/Python Learning Exchange
Welcome to join my thousand People Exchange learning questions: 125240963
Senior programmer: Deep into Python interprocess communication principle!