I
I
Issue2021-11-06 22:20:02
Python
Issue, 2021-11-06 22:20:02

How does async work in python?

from random import *
import sys,os,asyncio
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow

port = str(randint(9000, 9999))
url = "http://localhost:"+port
icon = "icon.ico"
title = "Qt Web App"
dw = 1920
dh = 1080
ww = 800
wh = 600

async def run_server():
    os.system("php -S localhost:"+port+" > started.log")

async def run_window():
    app = QApplication(sys.argv)
    web = QWebView()
    web.load(QUrl(url))
    web.setGeometry((dw-ww)/2, (dh-wh)/2, ww, wh)
    web.setWindowTitle(title)
    web.setWindowIcon(QIcon(icon))
    web.show()
    sys.exit(app.exec_())

run_server()
run_window()


There are two functions in the source code, one starts the php server, the second one should display a window to connect to the server and view the web application. How I just didn’t play with this asyncio , I just don’t understand how it should work ...

Please explain to the teapot how to start the server and not expect anything from it, but what if it also ends when the program is closed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-06
@paulenot

asyncio is rather how to do without multithreading.
It works well for situations where most of the execution thread's time is spent waiting for I/O - for example, a response over the network. If you have something that takes up the CPU, or any other operation that is performed outside of the scope of I / O through asyncio - this will stop the execution of the asynchronous program.
If on the fingers, then the asynchronous program consists of separate coroutines. The difference with threads is that threads switch OS, and coroutines themselves give up control when they go to wait for an event (roughly speaking, when they make a call with await). This simplifies the synchronization of coroutines, since you know exactly when your coroutine will give up control - there is less chance of running into a race condition due to a sudden switch. Again, coroutine switching is much more deterministic than thread switching, so floating bugs are less common.
The body of an asynchronous program is a work loop (the so-called reactor, aka loop), which checks the I / O operations in progress, and resumes the execution of the coroutine when its operation has completed. When the coroutine completes or schedules another operation, it will return control to the loop. That's why a "thinking" coroutine will stop the whole program.
The downside is that, unlike multithreading, you can't just buzz and make the code asynchronous. It should be written as asynchronous from the very beginning, with an eye to execution in the reactor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question