Skip to main content

Easy threading and multiprocessing for GUI applications

Project description

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

History

0.1.0 (2013-04-06)

  • initial PyPI release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_gui-0.1.0.tar.gz (20.5 kB view details)

Uploaded Source

File details

Details for the file async_gui-0.1.0.tar.gz.

File metadata

  • Download URL: async_gui-0.1.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for async_gui-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8903c905d238747d458c77feac93a311fe97926fb9f5c1a17607b5180c883aaf
MD5 aa6fea83623eac1730a571112abbcb2d
BLAKE2b-256 e79aacd4e1803841e2bb11172da37884a54ea54d96636303c7c99f35e510c1c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page