[PyQt] PyQt coroutines implementation

Kirill Kostyuchenko kisel2626 at gmail.com
Tue Nov 9 17:40:05 GMT 2010


Hello Phil.

I was impressed with the python coroutines idea and wrote small
implementation for the PyQt: http://github.com/ddosoff/pyqtcoroutines

Nonpreemptive multithreading programs looks synchronous:

from pyqtcoroutines.coroutines import Scheduler

# coroutine example
def inserter( a_lot_of_records ):
    try:
        for record in a_lot_of_records:
            # do small piece of work here

            ... insert one record to the sql database ...

            # This is execution trap
            #
            # Unlike QCoreApplication.processEvents()
            # we do not call new restricted event loop,
            # we will NORMALLY RETURN to the main qt event loop!

            yield
    except:
        # handle all exceptions in synchronous manner
        ...


# create scheduler to execute our inserter(..) later
s = Scheduler()

# It's not the inserter() call!
# We construct the built-in python Generator object!
#
# Execution will start after the first
# coroutine.send( None ) call inside the Scheduler.
coroutine = inserter( a_lot_of_records )

# Let the Scheduler to start execution of
# inserter(..) later inside the main qt event loop.
s.newTask( coroutine )

Full example : http://github.com/ddosoff/pyqtcoroutines
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101109/2774d5b0/attachment.html>


More information about the PyQt mailing list