Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:08:41+00:00 2026-06-09T03:08:41+00:00

I’m learning about Coroutine but it work strange that I can’t understand… Here’s the

  • 0

I’m learning about Coroutine but it work strange that I can’t understand…
Here’s the source

@coroutine
def printer():
    tmp=(yield)
    print tmp

def sender(coru):
    coru.send("hello")
    print "I'm sender"

if __name__=="__main__":
    coru=printer()
    sender(coru)
    print "I'm master"

shows

hello

StopItertationError @ coru.send(“hello”)

while,

@coroutine
def printer():
          while 1:
               tmp=(yield)
               print tmp

def sender(coru):
    coru.send("hello")
    print "I'm sender"

if __name__=="__main__":
    coru=printer()
    sender(coru)
    print "I'm master"

shows

hello

I’m sender

I’m master

correctly.

So I’m wondering

  1. why coroutine always work with the loops and why the first example rise error

  2. I heared about ‘yield from’ syntax at version 3.3. Is that help to make the first one work?

  3. I know every coroutine function work equivalently unlike the subroutines.

    but then, I think, after the Printer function ends, the program should be terminated without returning to Sender.

    but it does. doesn’t it means that sender superior to printer? what is the difference between subroutine and coroutine then.

thanks for reading and I really appreciate if you enlight me:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:08:42+00:00Added an answer on June 9, 2026 at 3:08 am

    When a generator (like printer) ends, it raises a StopIteration exception.

    When Python executes

    coru.send("hello")
    

    it jumps to

    tmp = (yield)
    

    and assigns “hello” to tmp. It then executes the next line:

    print tmp
    

    and then the generator ends, thus raising the StopIteration exception.

    If, on the other hand, you do not allow printer to end (by using a while 1 loop) then the StopIteration exception is never raised. Instead execution (in printer) continues until the next yield expression is reached:

    tmp = (yield)
    

    The send method returns the value of that yield expression (in this case, None). At this point, Python has once again returned to the sender function and next executes

    print "I'm sender"
    

    The purpose of the yield from syntax is to make it easier to refactor generators (intended to be used with send and throw) into subgenerators. See PEP380 and What’s New in Python3.

    It doesn’t change the StopIteration behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.