4

How to await this function (src ) in the main loop of pyside2:

async def do_request(value): #asyncqt maybe possible
    #print("do request")
    await asyncio.sleep(value)
    #print("request finished")
    return value

async def eventFilter(self, source, event): #impossible, needs pyside2 rewrite
     ... 

I am very reluctant to use any nonoffical stuff, so pyside2: i looked into examples of pyside2 having Qthreads examples, but no asyncio await. My lib uses asyncio so how to await in pyside2?

This is a serious issue: The https://github.com/harvimt/quamash/issues/104 does not support pyside2 and https://github.com/gmarull/asyncqt is not maintained. What is the solution?
Please how to integrate such simple call. i fear breaks/bugs on nonmaintained repos

2
  • 2
    I didn't vote either way, but you can always see the reason if you mouse over the downvote arrow. No one is obliged to give any further explanation than that. Commented Sep 21, 2019 at 19:59
  • 1
    I agree, this is some serious issue, and not an easy one to fix... takes someone with knowledge how to interoperate between different event loops. Commented Oct 30, 2022 at 11:49

3 Answers 3

1

after watching https://www.youtube.com/watch?v=ol5IuJyw-Tg using built in qthread qrunnable qthread to not block main gui thread is way to go.

for examples see https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/corelib and browse the source (its for pyside2)

Sign up to request clarification or add additional context in comments.

Comments

1

There are external libraries (asyncqt, qasync) to make Qt and asyncio play nicely, but such libraries seem to have poor support. The official PySide6 seems to have proper support with a module called QtAsyncio which is probably best (example here).

BUT this was affecting me in PySide2, and I found it best to simply run my asyncio event loop in a separate threading.Thread, with the Qt exec_ event loop in the main thread, and communicating between them with Qt Slots/Signals. That way could I fetch things in the background with the async/await syntax and keep the window running smoothly.

import threading
import asyncio
from PySide2.QtWidgets import QApplication, QLabel

async def foo():
    for i in range(10):
        label.setText(str(i))
        await asyncio.sleep(1)

app = QApplication()
label = QLabel()
label.show()
threading.Thread(target=lambda: asyncio.run(foo()), daemon=True).start()
app.exec_()

Hope it helps!

Comments

-1

PyQt is very popular. Though some may not want it because of licensing issues. Whether you choose PyQt or PySide, QTimer has some very nice functionality. In particular, you can use the singleShot() method to run code at a specific delay.

from PySide2.QtCore import QTimer

def myfunc():
    # do your stuff here
    pass

def do_request(self, value):
    # call static function singleShot
    QTimer.singleShot( delay_msec, myfunc)

you can also start() a QTimer to run a task at a regular interval.

2 Comments

would downvote: not realted to the async await lib and pyside2 is way to go since official.
you're right. there's no asyncio here. i was throwing out QTimer as it has some nice features that you might be able to use instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.