coroutine

Greenlet Vs. Threads

Greenlet Vs. Threads Question: I am new to gevents and greenlets. I found some good documentation on how to work with them, but none gave me justification on how and when I should use greenlets! What are they really good at? Is it a good idea to use them in a proxy server or not? …

Total answers: 4

Turn functions with a callback into Python generators?

Turn functions with a callback into Python generators? Question: The Scipy minimization function (just to use as an example), has the option of adding a callback function at each step. So I can do something like, def my_callback(x): print x scipy.optimize.fmin(func, x0, callback=my_callback) Is there a way to use the callback function to create a …

Total answers: 7

Equivalent C++ to Python generator pattern

Equivalent C++ to Python generator pattern Question: I’ve got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some manner. Python This is a …

Total answers: 13

How are generators and coroutines implemented in CPython?

How are generators and coroutines implemented in CPython? Question: I’ve read that in CPython, the interpreter stack (the list of Python functions called to reach this point) is mixed with the C stack (the list of C functions that were called in the interpreter’s own code). If so, then how are generators and coroutines implemented? …

Total answers: 2

Asynchronous method call in Python?

Asynchronous method call in Python? Question: I was wondering if there’s any library for asynchronous method calls in Python. It would be great if you could do something like @async def longComputation(): <code> token = longComputation() token.registerCallback(callback_function) # alternative, polling while not token.finished(): doSomethingElse() if token.finished(): result = token.result() Or to call a non-async routine …

Total answers: 14

Coroutine vs Continuation vs Generator

Coroutine vs Continuation vs Generator Question: What is the difference between a coroutine and a continuation and a generator ? Asked By: Mehdi Asgari || Source Answers: Coroutine is one of several procedures that take turns doing their job and then pause to give control to the other coroutines in the group. Continuation is a …

Total answers: 3

Is it safe to yield from within a "with" block in Python (and why)?

Is it safe to yield from within a "with" block in Python (and why)? Question: The combination of coroutines and resource acquisition seems like it could have some unintended (or unintuitive) consequences. The basic question is whether or not something like this works: def coroutine(): with open(path, ‘r’) as fh: for line in fh: yield …

Total answers: 5