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

  • Home
  • SEARCH
  • 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 822781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:50:50+00:00 2026-05-15T02:50:50+00:00

The greenlet package is used by gevent and eventlet for asynchronous IO. It is

  • 0

The greenlet package is used by gevent and eventlet for asynchronous IO. It is written as a C-extension and therefore doesn’t work with Jython or IronPython. If performance is of no concern, what is the easiest approach to implementing the greenlet API in pure Python.

A simple example:

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

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

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

Should print 12, 56, 34 (and not 78).

  • 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-05-15T02:50:50+00:00Added an answer on May 15, 2026 at 2:50 am

    This kind of thing can be achieved with co-routines which have been built-in to the standard Python distribution since version 2.5. If IronPython and co are fully compliant with all Python 2.5 features (I believe they are) you should be able to use this idiom.

    See this post for more information on how they can be used 🙂 Specifically, you’ll be interested in the PDF where the author builds a system using nothing but pure Python that provides similar capabilities to either stackless Python or the Greenlet module.

    You may also want to look either Gogen or Kamelia for ideas: these projects both have pure python coroutine implementations which you could either adopt or use as a reference for your own implementation. Take a look at this page for a gentle introduction to the cogen way of doing things.

    Note there are some differences between the co-routine implementations here and the greenletimplementation. The pure python implementations all use some kind of external scheduler but the idea is essentially the same: they provide you with a way to run lightweight, co-operative tasks without the need to resort to threads. Additionally both the frameworks linked to above are geared towards asynchronous IO very much like greenlet itself.

    Here’s the example you posted but rewritten using cogen:

    from cogen.core.coroutines import coroutine
    from cogen.core.schedulers import Scheduler
    from cogen.core import events
    
    @coroutine
    def test1():
        print 12
        yield events.AddCoro(test2)
        yield events.WaitForSignal(test1)
        print 34
    
    @coroutine
    def test2():
        print 56
        yield events.Signal(test1)
        yield events.WaitForSignal(test2)
        print 78
    
    sched = Scheduler()
    sched.add(test1)
    sched.run()
    
    >>> 12
    >>> 56
    >>> 34
    

    It’s a little more explicit than the greenlet version (for example using WaitForSignal to explicitly create a resume point) but you should get the general idea.

    edit: I just confirmed that this works using jython

    KidA% jython test.py 
    12
    56
    34
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My incomplete understanding is that Twisted, Stackless, Greenlet, Eventlet, Coroutines all make use of
Because of the way Eventlet, which Spawning depends on, installs itself, it can't be
I'm attempting to use the gevent library in a Python app I'm writing. However,
Python library gevent , version 0.13.6 (the current version on PyPI) will not pip
A (long) while ago I wrote a web-spider that I multithreaded to enable concurrent
Update: What I really wanted all along were greenlets . Note: This question mutated
Here I want to make some modificatins for my setting. I want response from
public class XPBN extends Activity{ private Map _map; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
I created map/reduce functions to group tasks results in one result object. I wrote
I use: MongoDB 1.6.5 Pymongo 1.9 Python 2.6.6 I have 3 types of daemons.

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.