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 8353465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:16:04+00:00 2026-06-09T09:16:04+00:00

I was implementing greenlet API just for practicing. from greenlet import greenlet def test1():

  • 0

I was implementing greenlet API just for practicing.

from greenlet import greenlet

def test1():
    print 12
    gr2.switch()
    print 34

def test2():
    print 56
    gr1.switch()
   print 78

gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()

here’s my messy code

def test1():
    tmp1=yield
    print 12
    try:
        gv2.send(1)
    except StopIteration:
        pass
    tmp1=yield
    print 34

def test2():
    tmp2=yield
    print 56
    try:
        gv2.send(1)
    except StopIteration:
        pass
    tmp1=yield
    print 78

gv1=test1()
gv1.next()
gv2=test2()
gv2.next()

gv1.send(1)

Shows,

12
56
Traceback (most recent call last):
  File "prog.py", line 26, in <module>
    gv1.send(1)
  File "prog.py", line 5, in test1
    gv2.send(1)
  File "prog.py", line 15, in test2
    gv2.send(1)
ValueError: generator already executing

So, I don’t know I guess correctly,

but It looks that after test1 send ‘1’ to test2, it still has something,

no control-flow switching happens unlike gevent. test1 still have the flow.

if not, I don’t understand what greenlet can do but python “coroutine” can’t exists.

My question is

  1. is python coroutine(yield-based) is the real thing(comparing it of others…lisp,ruby,&c)
  2. if right, would you please give some tips for that spagetti code?
  • 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-09T09:16:07+00:00Added an answer on June 9, 2026 at 9:16 am

    The generator instance of test2() is sending a value to itself.

    def test2():
        tmp2=yield
        print 56
        try:
            gv2.send(1) # this is the offending line
        except StopIteration:
            pass
        tmp1=yield
        print 78
    

    send() will resume the generator, but whenever code is executed inside test2(), it is already running. That is why it throws up.

    Did you want to do: gv1.send(1)? That would not work either.

    Here is why:

    • Current situation: before gv1.send(1) at the very end of your example
      • gv1 is dormant
      • gv2 is dormant
    • gv1.send(1) is invoked, this resumes gv1
    • gv1 proceeds to gv2.send(1)
    • this resumes gv2
    • gv2 proceeds to gv1.send(1)
    • gv1 is being resumed, however, gv1 has not reached a yield statement since it was last resumed. Ergo, it is still running, which is why it would also throw.

    Essentially, the difference could be summarized like this:

    • greenlets share an inherent connection with each other: .switch() pauses the currently executing greenlet and will resume however.
    • generators, on the other hand, are completely independent from each other. There is no shared context in which generators are executed.
      • yield will “pause” a generator
      • next()/ send() will resume a paused generator, invoking them on running generators will result in an exception.

    Why are you accessing gv2 (which represents one particular instance of test2) at all? The generator test2() should be self-contained and not make any assumptions about how it is being used. What if you decide, that you want to invoke the generator from some other scope? It doesn’t make any sense to send values to yourself anyway: You already have them.

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

Sidebar

Related Questions

implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing/handling idempotency in c# with Jolivers EventStore. Does this mean just taking care of
Implementing Interface just provide the skeleton of the method. If we know the exact
I implementing OLE DB provider for my custom database. It will be used from
While implementing the code from this question on my project I realized there's 3
Implementing centering feature for image downloads - Pop Up Window I posted here asking
Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written

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.