GithubHelp home page GithubHelp logo

pyqtcoroutines's Introduction

People like to think synchronously. Computers must work asynchronously.

This is implementation of nonpreemptive multithreading inside the python script using yield keyword.

Nonpreemptive multithreading

Insert yield as the execution trap or res = yield SomeClass() as the asynchronous call.

Write PyQt asynchronous code in the synchronous manner:

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 )

We could execute blocking calls asynchronously! Just inherit class Sleep from pyqtcoroutines.AsynchronousCall.

def async_sleeper():
    print 'sleep 100ms..'
    yield Sleep( 100 )  # Sleep - class, inherited from AsynchronousCall 
    print 'hello'

In addition to subcoroutines and system call David's ideas, I added yield Return(..) pattern.

def subcoroutine():
    # Network operations will be asyncronous,
    # but logic looks synchronous!
    #
    # After the download compleete, QT will notify 
    # DownloadSite instance with the signal
    # and execution will be resumed. 
    myWork = yield DownloadSite( 'http://google.com' )

    # Simply return value to caller's coroutine
    yield Return( myWork )


def coroutine():
    value = yield subcoroutine()
    ...

With the Qt signals feature we could return value from Task using signal done(..)

def coroutine():
    ... calc return value to res...
    yield Return( res )

class MyClass( QObject ):
    def resReady( self, res ):
        print res.value

l = MyClass()
s = Scheduler()
task = s.newTask( coroutine() )

# res will be printed, when coroutine done
task.done.connect( l.resReady )

Scheduler will route exceptions raised in the subcoroutines!

def subcoroutine():
    ... yield ... yield ...
    raise Exception( 'Something wrong' )
    ... yield ... yield ...


def coroutine():
    try:
        yield subcoroutine()
    except:
        # 'Something wrong' will be catched!
        ...

Coroutines asynchronously works with the only one thread.
Do not care about real threads, processes, ipc, syncronization primitives and hard debuging.

Check out working examples:

$ python ./coroutines.py

pyqtcoroutines's People

Contributors

ddosoff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.