Generator Case 2-using the generator to implement parallel effects on a single thread

Source: Internet
Author: User

#!\usr\bin\env\python

#-*-Coding:utf-8-*9

Import time

DEF consumer (name): #定义一个消费者模型

Print ("%s ready to eat"%name)

While True:

Baozi=yield

Print ("Something [%s] came up, was [%s] who ate"% (baozi,name))

C=consumer ("Pengdonghua")

C._next_ ()

def producer (name): #定义一个生产者模型

C=consumer (' A ') #定义两个消费者A, B

C2=consumer (' B ')

C._next_ ()

C2._next_ ()

Print ("Things ready to be made")

For I in range: #循环10次, 1-second delay print output

Time.sleep (1)

Print ("Made 2 eat Something")

C.send (i)

C2.send (i)

Generator Case 2-using the generator to implement parallel effects on a single thread

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.