S
S
solid4772021-11-08 15:48:58
Python
solid477, 2021-11-08 15:48:58

How to fix RuntimeWarning: coroutine was never awaited?

Good day. I need to loop 10 times in the woriking method, I am making a PyQt program and when I click on the button I get an error:

RuntimeWarning: coroutine 'Ui_MainWindow.working' was never awaited
sys.exit(app.exec_())
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

from datetime import datetime
from PyQt5 import QtCore, QtGui, QtWidgets
import asyncio
import requests

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(493, 267)
        font = QtGui.QFont()
        font.setFamily("WhitehallCyr")
        font.setPointSize(12)
        MainWindow.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setInputMethodHints(QtCore.Qt.ImhNone)
        MainWindow.setIconSize(QtCore.QSize(48, 48))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lblAPI = QtWidgets.QLabel(self.centralwidget)
        self.lblAPI.setGeometry(QtCore.QRect(20, 14, 91, 21))
        font = QtGui.QFont()
        font.setFamily("Copperplate Cyrillic")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        font.setStyleStrategy(QtGui.QFont.PreferDefault)


        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pushButton.clicked.connect(self.working)

    async def working(self):
        for i in range(5):
            try:
                x = requests.get('https://w3schools.com')
                print(x.status_code)
                break
            except:
                self.lblResText_2.setText("Ошибка!")


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Строка 1"))
        self.lblAP.setText(_translate("MainWindow", "Строка 2: "))
        self.lblA.setText(_translate("MainWindow", "Строка 3: "))
        self.lblS.setText(_translate("MainWindow", "Строка 4: "))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Can you please tell me how to make asynchronous loop execution in the amount of 10 times?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad Grigoriev, 2021-11-08
@Vaindante

It's just that asynchronous code will not work in synchronous code.
Plus minus what the error says, that the task was set, but never launched - an approximate free interpretation.
To make friends, you need to use asyncqt
, and in general, it would not be bad to tighten up the basics of python to begin with, before writing asynchronous programs
print, request - synchronous in your code, zero sense of asynchrony
, you don’t need to catch all exceptions either, there can be many side effects

V
Vindicar, 2021-11-08
@Vindicar

self.pushButton.clicked.connect(self.working)
Are you sure that the button click handler can be a coroutine (async function)?
In my opinion, there should be a normal function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question