Giter VIP home page Giter VIP logo

async_gui's Introduction

async_gui

async_gui is a library aimed to make use of threads in GUI applications simpler. It's inspired by PyCon talk Using futures for async GUI programming in Python 3.3 and tornado @gen.engine implementation.

Most of GUI toolkits don't allow you to access graphical elements from non-GUI thread. Python 3.2+ has nice new feature concurrent.futures, but we can't just wait for result from future and callbacks are not very handy.

Combination of Coroutines via Enhanced Generators (PEP-342) and futures creates a rich and easy to use asynchronous programming model which can be used for creating highly responsive GUI applications.

Example

Demo of button click handler:

@engine.async
def on_button_click(self, *args):
    self.status_label.setText("Downloading image...")
    # Run single task in separate thread
    image_data = yield Task(self.load_url,
                            "http://www.google.com/images/srpr/logo4w.png")
    pixmap = QtGui.QPixmap.fromImage(QtGui.QImage.fromData(image_data))
    self.image_label.setPixmap(pixmap)
    self.status_label.setText("Downloading pages...")
    urls = ['http://www.google.com',
            'http://www.yandex.ru',
            'http://www.python.org']
    # Run multiple task simultaneously in thread pool
    pages = yield [Task(self.load_url, url) for url in urls]
    self.status_label.setText("Done")
    avg_size = sum(map(len, pages)) / len(pages)
    self.result_label.setText("Average page size: %s" % avg_size)

Tasks yielded from on_button_click() executed in thread pool, but GUI updates done in the GUI thread. For CPU-bound applications there is also ability to run tasks in pool of processes.

See full example in examples directory.

Features

  • Python 2.7+ (futures required), Python 3+ support
  • PyQt4/PySide, Tk, Wx, Gtk GUI toolkits support. Easy to add another
  • Can execute tasks in Thread, Process, Greenlet (gevent required)
  • Possibility to run multiple tasks at the same time
  • Straightforward exception handling
  • Full test coverage

Installation

Using pip:

$ pip install async_gui

Or download, unpack and:

$ python setup.py install

To run tests use:

$ python setup.py test

Links

async_gui's People

Contributors

reclosedev avatar timeyyy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

async_gui's Issues

QCoreApplication.processEvents got QEventLoop.ProcessEventsFlag instead of QEventLoop.ProcessEventsFlags (should be plural?)

I got this traceback while runn async_gui in a Pyside app:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\async_gui\engine.py", line 81, in wrapper
    return self.create_runner(gen).run()
  File "C:\Python27\lib\site-packages\async_gui\engine.py", line 136, in run
    task = self._execute_single_task(gen, executor, task)
  File "C:\Python27\lib\site-packages\async_gui\engine.py", line 149, in _execute_single_task
    self.engine.update_gui()
  File "C:\Python27\lib\site-packages\async_gui\toolkits\_qt.py", line 14, in update_gui
    int(self.pool_timeout * 1000)
TypeError: arguments did not match any overloaded call:
  QCoreApplication.processEvents(QEventLoop.ProcessEventsFlags flags=QEventLoop.AllEvents): argument 1 has unexpected type 'PySide.QtCore.QEventLoop.ProcessEventsFlag'
  QCoreApplication.processEvents(QEventLoop.ProcessEventsFlags, int): argument 1 has unexpected type 'PySide.QtCore.QEventLoop.ProcessEventsFlag'

I check the Qt doc the argument should be QEventLoop.ProcessEventsFlags, but I got QeventLoop.ProcessEventsFlag (single not plural) instead.

When an exception occurs in a future, we lose most of the traceback

Consider the following:


def bad():
    worse()

def worse():
    raise RuntimeError("Bad Day")

Currently, if this code gets executed in a Task, we'll lose the traceback, only the last line will show up.
When I ran across this problem with Python Futures in my own code, I came up with a janky solution with a subclassed Executor class, perhaps you can think of something better than this for async_gui, which is awesome btw, thank you.

class CustomExecutor(concurrent.futures.thread.ThreadPoolExecutor):

    def submit(self, task, *args, **kwargs):
        @functools.wraps(task)
        def task_wrapper():
            try:
                return task(*args, **kwargs)
            except Exception as e:
                logger.exception("Error executing %r with args %r and kwargs %r" % (task, args, kwargs))
                raise
        future = super(CustomExecutor, self).submit(task_wrapper)
        return future

update pypi

Would we able to get a pypi update with the kivy version?

Async functions raise StopIteration unless they yield

This example async function will raise StopIteration if called, instead of just returning:

@async
def test():
    print "Don't need to yield"
    return
    yield Task(hello_world)

def hello_world():
    print "Hello world"

My initial try at a fix is to subclass Runner and my desired engine to ignore StopIteration.

class CustomRunner(Runner):

    def run(self):
        try:
            return super(CustomRunner, self).run()
        except StopIteration:
            pass

class CustomEngine(Engine):

    def create_runner(self, gen):
        return CustomRunner(self, gen)

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.