Skip to main content

Use 'await' syntax with Qt signals

Project description

qt-await

This lets you use Python's async & await syntax with PyQt or PySide applications, to wait for Qt signals.

It doesn't integrate Qt with asyncio, trio, or code written for those frameworks.

Usage

To wait for arbitrary signals, wrap them in a SignalQueue. For example, using the QProcess.finished signal:

from PyQt5 import QtCore, QtWidgets
from qt_await import SignalQueue

class MainWindow(QtWidgets.QMainWindow):
    ...

    async def run_subprocess(self):
        proc = QtCore.QProcess()
        sq = SignalQueue(proc.finished)
        proc.start("sleep", ["2"])
        exit_code, exit_status = await sq

There are some helper functions to make common operations easier:

# Pause for 1s (all times are ms, following Qt's convention)
await sleep(1000)

# Pause repeatedly
async for _  in sleep_loop(1000):
    ...

# Wait for something else, with a time limit (also in ms)
await with_timeout(..., 3000)

# Run a QProcess & wait for its finished signal
await run_process(proc, "sleep", ["2"])

# Get streaming bytes from a QIODevice (e.g. a process or a socket)
async for b in read_streaming_bytes(...):
    dest.write(b)

# Similar, but decoding bytes output to strings
async for s in read_streaming_text(...):
    print(s, end='')

All this code needs to be in async def functions, so there are two extra functions to get from normal Qt code into async:

  • start_async(f()) starts an async function immediately.
  • connect_async(signal, f) connects an async function to a PyQt signal, to start whenever the signal is emitted.

Limitations

This is an experiment, which I mostly wrote for fun - use it at your own risk.

This doesn't integrate with asyncio (see qasync for that) or trio (see qtrio), or any other Python async libraries. It assumes you're doing things the Qt way - QProcess for subprocesses, QThread for threads, QNetworkRequest for HTTP, and so on. It aims to fit in with these APIs as much as possible.

It's also not really an async framework itself. If you want to write significant amounts of async code in Python, trio or asyncio will provide more robust machinery & better abstractions. qt_await just lets you sprinkle a bit of await in your Python Qt code.

Another thing you might want is the qt-async-threads package, which lets you wrap arbitrary slow Python functions in a thread and await their completion, to avoid blocking the event loop.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

qt_await-0.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

qt_await-0.1-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file qt_await-0.1.tar.gz.

File metadata

  • Download URL: qt_await-0.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for qt_await-0.1.tar.gz
Algorithm Hash digest
SHA256 16de6fa483fe39d7cd4930c7bffadf5ac04c7ed4900a1d0200e17080b1eab7fb
MD5 3e7d129d81dd3bd63f111724043e9261
BLAKE2b-256 7f5da811b3ac6a22ae915eb7ebaa107d4638433c354ae9e13d264360e75d9fd1

See more details on using hashes here.

Provenance

File details

Details for the file qt_await-0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: qt_await-0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for qt_await-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c2b1d5c4f34f86adc7c4cc373a6d6708d047708fd4ff5669bce93443527cceaa
MD5 6a13d545a48cb41d2c40f5aa7a2c9731
BLAKE2b-256 4650cca837b1f2f49d5943172d542cd068237a59968d07cf3bf6c6f9835f4b58

See more details on using hashes here.

Provenance

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