Python asyncio: Führen Sie eine asynchrone Funktion aus, während der Hauptfaden blockiert ist

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Python asyncio: Führen Sie eine asynchrone Funktion aus, während der Hauptfaden blockiert ist

by Anonymous » 29 Jul 2025, 06:47

Ich kodiere ein Python -Projekt, das in zwei Teile aufgeteilt ist: < /p>

Eine pyside6 -GUI -Schnittstelle, die synchron auf dem Hauptfaden -Faden laufen muss, und folglich blockieren Sie es. Reihenfolge der Ausführung der asynchronen Funktion.

Code: Select all

async def run_app():
global page_manager, gui_manager
pw: Playwright | None = None

try:
# Those three following are quick and do not cause any significant time loss
pw = await async_playwright().start()
page_manager = PageManager(pw)
gui_manager = GUIManager()

# The `page_manager.init(settings.BROWSER)` should be starting about here

gui_manager.init()  # This is a prerequisit for the next line
page_url, save_path = gui_manager.run_sync()  # This blocks the main thread

await page_manager.init(settings.BROWSER)  # This function should be executed during the GUI
await page_manager.run(page_url) # This is ran after the end of `gui_manager.run_sync()`
except Exception as e:
logger.critical(f"An error occurred (exiting) : {str(e)}")
raise
finally:
gui_manager.close()
await page_manager.close()
if pw:
await pw.stop()

if __name__ == "__main__":
asyncio.run(run_app())
Der Programm beginnt derzeit mit der GUI und , wenn der Benutzer seine Auswahl durchführte. entweder. Bibliothek und Asyncio Bibliothek.

Top