qasync

0.28.0 · active · verified Wed Apr 15

qasync is a Python library for using asyncio in Qt-based applications, allowing developers to seamlessly integrate asynchronous code into their Qt GUI projects. It is currently at version 0.28.0 and maintains a regular release cadence with updates for new Python versions and performance improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `asyncio` with a basic Qt application using `qasync`. It shows how to set the `QEventLoopPolicy`, create an `asyncSlot` for a button click, and run an `asyncio` task without blocking the GUI.

import asyncio
import sys

# Choose your Qt binding: PyQt5, PyQt6, PySide2, PySide6
# For this example, we'll use PyQt5
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QPushButton

from qasync import QEventLoopPolicy, asyncSlot

async def long_running_task(duration_seconds: int = 2):
    """A mock asynchronous task."""
    print(f"[Task] Starting long-running task for {duration_seconds} seconds...")
    await asyncio.sleep(duration_seconds)
    print("[Task] Long-running task finished!")
    return "Task Completed!"

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("qasync Quickstart Demo")
        self.setGeometry(100, 100, 400, 200)

        self.layout = QVBoxLayout()
        self.label = QLabel("Click the button to start an async task.")
        self.button = QPushButton("Run Async Task")

        # Connect a Qt signal to an asynchronous slot using @asyncSlot
        self.button.clicked.connect(self.run_task)

        self.layout.addWidget(self.label)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

    @asyncSlot()
    async def run_task(self):
        """This slot runs an async task without blocking the GUI."""
        self.label.setText("Task started, please wait...")
        self.button.setEnabled(False) # Disable button while task is running

        result = await long_running_task(3) # Await the async task
        
        self.label.setText(f"Result: {result}")
        self.button.setEnabled(True) # Re-enable button

if __name__ == "__main__":
    # IMPORTANT: Set the asyncio event loop policy *before* creating QApplication
    asyncio.set_event_loop_policy(QEventLoopPolicy())

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    
    # Run the Qt application
    sys.exit(app.exec_())

view raw JSON →