PySide6按钮点击时异步协程无法运行问题排查
PySide6异步按钮点击无响应排查求助
问题概述
在PySide6项目中,尝试实现点击按钮触发异步协程,通过aiohttp获取数据。已使用qasync的@asyncSlot()装饰器,但点击按钮后无任何反应,连print语句都不执行。调整asyncio运行逻辑后依旧无效,且无报错信息。其他普通按钮点击方法可正常运行,仅异步逻辑存在问题。
补充排查:程序卡在aiohttp请求阶段,get_data内的print也未执行。
代码实现
主窗口代码
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.container = QWidget() # Controls container widget. self.controlsLayout = QVBoxLayout() # -- Search bar and settings button self.search_layout = QHBoxLayout() self.search_bar = QLineEdit() self.search_button = QPushButton("Search") self.search_button.clicked.connect(self.update_data) # search by entered text self.search_layout.addWidget(self.search_button) self.controlsLayout.addLayout(self.search_layout) # Our tab container with adjustable number of tabs self.tab_container = TabContainer() # Add out tabs widget self.controlsLayout.addWidget(self.tab_container) self.container.setLayout(self.controlsLayout) self.setCentralWidget(self.container) @asyncSlot() async def update_data(self): stock_tickers = self.search_bar.text() print("Entered tickers: ", stock_tickers) short_interest = asyncio.create_task(self.setup_data(stock)) short_interest = await short_interest print('SHORT INTEREST:', short_interest) # Make a table in a new tab here # .............................. async def setup_data(self, stock): short_interest = await get_data(stock) # this function is our main coroutine # process the data here ...... as previous method return json async def main(): app = QApplication(sys.argv) loop = qasync.QEventLoop(app) asyncio.set_event_loop(loop) main_window = MainWindow() main_window.show() sys.exit(app.exec()) if __name__ == '__main__': asyncio.run(main())
aiohttp请求函数
async def get_data(stock_ticker): async with aiohttp.ClientSession() as session: async with session.get('https://www.finra.org/finra-data/browse-catalog/equity-short-interest/data') as response: cfruid = session.cookie_jar.filter_cookies('https://www.finra.org/')["__cfruid"].value print("CFRUID", cfruid) five_months_date = date.today() + relativedelta(months=-5) headers = { 'authority': 'services-dynarep.ddwa.finra.org', 'accept': 'application/json, text/plain, */*', 'accept-language': 'en-US,en;q=0.6', 'content-type': 'application/json', 'cookie': f'XSRF-TOKEN={cfruid};', 'origin': 'https://www.finra.org', 'referer': 'https://www.finra.org/', 'x-xsrf-token': cfruid, } json_data = { 'fields': [ 'settlementDate', 'issueName', 'symbolCode', 'marketClassCode', 'currentShortPositionQuantity', 'previousShortPositionQuantity', 'changePreviousNumber', 'changePercent', 'averageDailyVolumeQuantity', 'daysToCoverQuantity', 'revisionFlag', ], 'dateRangeFilters': [], 'domainFilters': [], 'compareFilters': [ { 'fieldName': 'symbolCode', 'fieldValue': 'GME', 'compareType': 'EQUAL', }, { 'fieldName': 'settlementDate', 'fieldValue': str(five_months_date), 'compareType': 'GREATER', }, ], 'multiFieldMatchFilters': [], 'orFilters': [], 'aggregationFilter': None, 'sortFields': [ '-settlementDate', '+issueName', ], 'limit': 50, 'offset': 0, 'delimiter': None, 'quoteValues': False, } async with session.post('https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/group/OTCMarket/name/ConsolidatedShortInterest', headers=headers, json=json_data) as response2: short_interest_data = await response2.json() return short_interest_data
额外说明
该get_data协程在单独文件中通过创建任务并await可正常运行并返回数据,但嵌入PySide6项目后就出现卡顿无响应的情况。
内容的提问来源于stack exchange,提问作者David Frick
相关产品推荐
相关产品推荐

