Python Generator: Single Cheng

Source: Internet
Author: User

1 #author F2 3 #parallel effect on single thread (generator)4 #co-process5 6 Import Time7 defConsumer (name):8     Print("%s prepare to eat steamed buns."%name)9      whileTrue:TenBaozi =yield One         Print("steamed bun%s has been eaten by%s!"%(Baozi, name)) A  -c = Consumer ("WW")#The consumer is not a function at this point. He's a generator. It just initializes a generator. - #c.send ("[Beef meat]") theC.__next__()#you have to execute a Next statement before you get to yield. -C.send ("[Pork meat]")#send syntax to pass the parameter to the generator -  - defproducer (name): +c = Consumer ("A") -C2 = Consumer ("B") +C.__next__() AC2.__next__() at     Print("Lao Tzu began to prepare buns!") -      forIinchRange (1, 11): -Time.sleep (1) -         Print("made a stuffed bun! Two people share food") - c.send (i) - c2.send (i) in  -Producer"F")

Python Generator: Single Cheng

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.