Как организовать многопоточность
У меня есть код, лезет WARNING: QApplication was not created in the main() thread, но код выполняется, только вот 1 раз, когда начинает выполняться 2 раз сайт помирает, как можно организовать многопоточность, что бы он не помирал?
from django.shortcuts import render
from .forms import UserForm
from bs4 import BeautifulSoup
from django import forms
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEnginePage
import sys
import time
import re
import requests
regex = r"price_85d2b9c\s+\w+\D+(?P<price>\d+\s+\d+)"
class Client(QWebEnginePage):
def __init__(self,url):
global app
self.app = QApplication(sys.argv)
QWebEnginePage.__init__(self)
self.html = ""
self.loadFinished.connect(self.on_load_finished)
self.load(QUrl(url))
self.app.exec_()
def on_load_finished(self):
self.html = self.toHtml(self.Callable)
print("Load Finished")
def Callable(self,data):
self.html = data
self.app.quit()
def index(request):
submitbutton= request.POST.get("submit")
From=''
To=''
url=''
prices = []
form= UserForm(request.POST or None)
if form.is_valid():
From = (form.cleaned_data.get("From")).partition(", ")[2]
To = (form.cleaned_data.get("To")).partition(", ")[2]
regex = r"price_85d2b9c\s+\w+\D+(?P<price>\d+\s+\d+)|price_85d2b9c\D+(?P<pric>\d+\s+\d+)"
url = "https://www.aviasales.ru/search/"+From+"1012"+To+"23121?request_source=search_form&expected_price_currency=rub&expected_price_source=calendar"
print(url)
client_response = Client(url)
#print(client_response.html)
test_str = client_response.html
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
prices.append("{group}".format(group = match.group(groupNum)))
context= ({'form': form, 'From': From,
'To': To, 'submitbutton': submitbutton,'prices': prices})
return render(request, "index3.html", context)```