[Twisted-Python] Idiom for database access within iterator/generator?

Manlio Perillo manlio_perillo at libero.it
Tue Jun 3 14:14:23 EDT 2008

Justin Warren ha scritto:
> Greetings twisted gurus!
> I'm hoping someone with a better algorithmic brain than I could offer
> some advice on a good way to do something in a twisty/pythonic way.
> The pseudocode for what I want to do is:
> for item in generator_that_fetches_rows_from_database:
> 	...
> 	do_stuff()
> 	...
> where generator_that_fetches_rows_from_database is an object that uses
> enterprise.adbapi (or anything else) to fetch rows from a database and
> return them to the for..in.. loop.

This can be done, but you need a "pure" asynchronous PostgreSQL client.
Here is an implementation in Twisted:

Each row is processed using the IRowConsumer interface.

To keep it simple, a callback function is called for each row received 
from the backend.

Using Python 2.5 generators you can (probabily) achieve what you want.

 > [...]

Manlio Perillo

More information about the Twisted-Python mailing list