Python road Pythonthread, third, process 3

Source: Internet
Author: User

Python3 Process 3

Pipeline

Creates a pipe space in memory that is visible to multiple processes. Form a constraint in the form of communication;

Linux file types

b c D-l s p

Directory plain file Link socket pipeline

Multiprocessing-----> Pipe functions

Pipe (Duplex)

Function: Create a pipeline

Parameter: Duplex The default is true to indicate that the pipeline is a two-way pipe (full duplex);

If set to False, the pipe is a one-way pipe;

Return value: Returns two pipe flow objects, assigned to represent both ends of the pipe;

If the parameter is true (the default), two objects can be sent to receive;

If False, the first object can only be received, and the second object can only be sent;

Description: 1, send data to the pipeline using the Send () function, receive data from the pipeline using the recv () function;

The 2,RECV () function is a blocking function that blocks when the data in the pipeline is empty;

3, once recv (), can only receive the contents of a Send ();

4,send () can send multiple types of data, such as a string number list;

1  fromMultiprocessingImportProcess,pipe2 ImportOs,time3 4 #To create a pipe object5 #when the argument is false, child can only recv the parent only send6 #child_conn,parent_conn = Pipe (False)7 8Child_conn,parent_conn =Pipe ()9 Ten #Child process Functions One defFun (name): ATime.sleep (1) -     #send a string to the pipe -Child_conn.send ('Hello'+STR (name)) the     Print(Os.getppid (),"----", Os.getpid ()) -  -Jobs = [] - #Create 5 sub-processes +  forIinchRange (5): -p = Process (target = Fun,args =(i,)) + jobs.append (P) A P.start () at  -  forIinchRange (5): -data =parent_conn.recv () -     Print(data) -  -  forIinchJobs: inI.join ()
View Code

Message Queuing:

Multiprocessing---> Queue

A queue model is created in memory to hold messages, and any process that has queue formation can store and remove messages;

Queue (maxsize = 0)

Function: Create a message Queue object;

Parameters: MaxSize Default = 0 indicates that Message Queuing can hold messages determined by the system's automatically allocated space;

A positive integer of > 0 indicates the maximum number of messages stored in the queue;

Return value: Message Queue object;

Q.put ()

1, a message is stored in the message queue, and when the message queue is full, it will block;

2, the type of messages stored can be numbers, lists, strings, etc.;

Q.full ()

Determines whether the queue is full, returns true if full, otherwise returns false;

Q.qsize ()

View the number of messages in the current queue;

Q.get ()

Gets the message, gets one at a time, or blocks when the message queue is empty;

Description: The block parameter and the timeout parameter in put (), get ()

Block defaults to True, indicating that two functions are blocking functions;

If set to false, it means no blocking;

Timeout when the block is set to true, the timeout wait time is indicated;

1  fromMultiprocessingImportQueue2 3 #To create a Message Queuing object4Q = Queue (3)5 6i =07 #Store Messages8  whileTrue:9     #determine if the queue is fullTen     ifq.full (): One         Print("Queue is full") A          Break -Q.put ("Hello"+str (i)) -i + = 1 the  - Print("The current queue has%d messages"%q.qsize ()) -  -  forIinchRange (Q.qsize ()): +     Print("Get message%s"%q.get ()) -  + Print("Is empty?", Q.empty ()) A  at Print(Q.get (true,3)) -  - Print("process Over")
View Code

Python road Pythonthread, third, process 3

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.