Skip to content

nicoddemus/qt-async-threads

Repository files navigation

qt-async-threads

image

image

image

image

pre-commit.ci status

image

image


qt-async-threads allows Qt applications to use convenient async/await syntax to run computational intensive or IO operations in threads, selectively changing the code slightly to provide a more responsive UI.

The objective of this library is to provide a simple and convenient way to improve UI responsiveness in existing Qt applications by using async/await, while at the same time not requiring large scale refactorings.

Supports PyQt5, PyQt6, PySide2, and PySide6 thanks to qtpy.

Example

The widget below downloads pictures of cats when the user clicks on a button (some parts omitted for brevity):

This works well, but while the pictures are being downloaded the UI will freeze a bit, becoming unresponsive.

With qt-async-threads, we can easily change the code to:

By using a QtAsyncRunner instance and changing the slot to an async function, the runner.run calls will run the requests in a thread, without blocking the Qt event loop, making the UI snappy and responsive.

Thanks to the async/await syntax, we can keep the entire flow in the same function as before, including handling exceptions naturally.

We could rewrite the first example using a ThreadPoolExecutor or QThreads, but that would require a significant rewrite.

Documentation

For full documentation, please see https://qt-async-threads.readthedocs.io/en/latest.

Differences with other libraries

There are excellent libraries that allow to use async frameworks with Qt:

Those libraries fully integrate with their respective frameworks, allowing the application to asynchronously communicate with sockets, threads, file system, tasks, cancellation systems, use other async libraries (such as httpx), etc.

They are very powerful in their own right, however they have one downside in that they require your main entry point to also be async, which might be hard to accommodate in an existing application.

qt-async-threads, on the other hand, focuses only on one feature: allow the user to leverage async/await syntax to handle threads more naturally, without the need for major refactorings in existing applications.

License

Distributed under the terms of the MIT license.

About

Use convenient async/await syntax to spawn threads in Qt applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages