Example of pipeline usage in python

Source: Internet
Author: User

Example of pipeline usage in python

This example describes how to use pipelines in python. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#! Coding = UTF-8

Import multiprocessing

Def consumer (pipe ):

Output_p, input_p = pipe

Input_p.close () # Close the input of the Pipeline

While True:

Try:

Item = output_p.recv ()

Failed t EOFError:

Break

Print item

Print ("consumer done ")

# Production project and put it on the queue

Def producer (sequence, input_p ):

For item in sequence:

Input_p.send (item)

If _ name _ = "_ main __":

# Creating an MPS queue

(Output_p, input_p) = multiprocessing. Pipe ()

# Start the user process

Cons_p = multiprocessing. Process (target = consumer, args = (output_p, input_p ),))

Cons_p.start ()

# Close the output pipeline in the producer

Output_p.close ()

# Production Project

Sequence = [1, 2, 4]

Producer (sequence, input_p)

# Close the input media transcoding queue, indicating that the input media transcoding queue is complete.

Input_p.close ()

# Waiting for the user to close

Cons_p.join ()

I hope this article will help you with Python programming.

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.