[PyQt] PyQt coroutines implementation

Phil Thompson phil at riverbankcomputing.com
Sun Dec 12 10:37:00 GMT 2010


On Tue, 9 Nov 2010 22:40:05 +0500, Kirill Kostyuchenko
<kisel2626 at gmail.com> wrote:
> 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

Interesting...

I'd be interested to know people's opinion of the new futures module...

http://docs.python.org/dev/py3k/library/concurrent.futures.html#module-concurrent.futures

...and how it compares with QtConcurrent.

It might be possible to write an Executor that wraps the QtConcurrent
implementation - or maybe QtConcurrent doesn't add anything to what
concurrent.futures provides.

Phil

Phil


More information about the PyQt mailing list