Python Learning notes-Queue & Pipes, interprocess communication

Source: Internet
Author: User

It says how Python creates multiple processes, but the process created in the previous article is dumb and deaf, self-executing and does not communicate with each other.
So how do you let the process talk to each other?
Python provides us with a function multiprocessing.pipe
And a class: multiprocessing. Queue.

Multiprocessing. Pipe ()

multiprocessing.Pipe()That is, pipeline mode, called pipe (), returns the connection at both ends of the pipe.

a pair (conn1, conn2) of Connection objects representing the ends of a pipe.

Therefore, pipe is only suitable for single-duplex cases where only two processes read and write, meaning that the information flows in only one direction. For example, television, radio, television viewers can only see, television is able to broadcast television.

The pipe reads and writes more efficiently than the queue.
The pipe between processes is built on the fork mechanism.
When the main process creates a pipe, the two connections of the pipe are connected to the main process.
When the master process creates a child process, connections is also copied. At this time there are 4 connections.
After that, close an out Connection of the main process and close one in Connection of the child process. Then build up a pipeline of input in the main process that outputs the child process.
The principle is as follows:

With more information can read: Http://www.tuicool.com/articl ...

# sample Code# Coding=utf-8From multiprocessingImport Pipe, ProcessDefSon_process(x, pipe): _out_pipe, _in_pipe = pipe# Turn the fork over the input end _in_pipe.close ()Whiletrue: try:msg = _out_pipe.recv ()  Print msg except eoferror: # when Out_pipe is not receiving output and the input is closed, Will throw Eorferror, can capture and exit child process breakif __name__ =  __main__ ': out_pipe, in_pipe = pipe (true) son_p = Process (target=son_process , args= (100, (Out_pipe, In_pipe))) Son_p.start () # etc. pipe is fork, Close the output of the main process # this way, one end of the created pipe is connected to the input of the main process, and one end is connected to the output port of the child process Out_pipe.close () for x in range (1000): In_pipe.send (x) In_pipe.close () son_p.join () print  "main process also ended"  

To summarize:

    • The above code mainly uses the Send (), recv (), close () method of the pipe. Eoferror is thrown when the input of the pipe is closed and the input value cannot be received.

    • When creating a new pipe (duplex), if duplex is true, the pipeline created is bidirectional, and if duplex is false, the pipeline created is one-way.

Multiprocessing. Queue

The queue is also based on the official documentation for pipe implementations.
The use of the queue is mostly one side put (), and one side get (). However, a queue can be a put operation for multiple process or a get () operation from multiple process.
Demo:

# Coding=utf-8From multiprocessingImport Queue, ProcessFrom QueueImport EmptyAs QueueemptyImport RandomDefGetter(name, queue):Print' Son process%s '% nameWhileTrue:Try:value = Queue.get (True,10)# block is true if there is no data in the queue.# | —————— If timeout is none by default, it will wait forever.# | —————— If timeout is set for time, the Queue.empty exception will be thrown after timeout seconds# block is False, throws a Queue.empty exception if there is no data in the queuePrint"Process getter get:%f"% valueExcept Queueempty:BreakDefPutter(name, queue):Print"Son process%s"% nameFor Iin range (0, 1000): value = Random.random () queue.put (value) # put data put (obj[, block[, timeout]]) # If block is true, such as queue is full: # | —————— If timeout is the default none, then it will wait until # | —————— If timeout sets the wait time, it waits for timeout seconds and then throws Queue.full if it is full. # if block is false, if the queue is full, directly throw queue.full print " Process putter put:%f "% valueif __name__ =  __ main__ ': queue = queue () getter_process = Process (Target=getter, args= ( "getter", queue)) putter_process = Process (Target=putter, args= ( "putter", queue)) Getter_process.start () Putter_process.start ()             

Some instructions for the queue have been written in the code.

Python Learning notes-Queue & Pipes, interprocess communication

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.