Python 在django中运行页面提交的并行进程
我想创建一个简单的pnr检查网站。代码工作正常,但一次只执行一项工作,要么将结果呈现给其他页面,要么发送邮件。当使用线程时,它会在后端发送邮件,直到页面保持加载状态。 请任何人给我建议。我还想在谷歌appengine上运行它,所以我没有试过芹菜Python 在django中运行页面提交的并行进程,python,django,parallel-processing,Python,Django,Parallel Processing,我想创建一个简单的pnr检查网站。代码工作正常,但一次只执行一项工作,要么将结果呈现给其他页面,要么发送邮件。当使用线程时,它会在后端发送邮件,直到页面保持加载状态。 请任何人给我建议。我还想在谷歌appengine上运行它,所以我没有试过芹菜 from django.http import HttpResponse from bs4 import BeautifulSoup import re import requests from django.shortcuts import rende
from django.http import HttpResponse
from bs4 import BeautifulSoup
import re
import requests
from django.shortcuts import render
from functools import partial, wraps
from django.core.mail import send_mail
import time
import thread
def checkpnr(request):
return render(request, 'checkpnr.html')
def check(string, pnr, sleeptime, lock, *args):
while 1:
# entering critical section
lock.acquire()
# time1=request.get_all("notif")
url_pnr = "pnr url"
r = requests.get(url_pnr)
data = r.text
soup = BeautifulSoup(data)
train = str(soup.find("ul", attrs={"class": "train_info"}))
train_number = soup.find("li", attrs={"class": "first"}).text
source = str(soup.find("travellers"))
route = str(soup.findAll("li")[1]).replace(
'<li>', '').replace('</li>', '')
#head, sep, tail = route.partition(' -')
travel_date = str(soup.findAll("li")[2].text)
date, sep, total = travel_date.partition('|')
rows = soup.findAll("td", attrs={"class": "pax"})
rowlength = len(rows)
chart_status = str(soup.findAll("tr")[rowlength + 1].findAll("td")[0]).replace(
'<td colspan="3"><strong>', '').replace('</strong>', '').replace('</td>', '')
passengers = []
status = []
coach = []
tot = []
w=''
i = 1
j = 1
while i <= rowlength:
j = str(soup.findAll("tr")[i].findAll(
"td")[0].text).replace(':', '')
passengers.append(j)
s = str(soup.findAll("tr")[i].findAll("td")[1].text)
w=w+','+s
status.append(s)
c = str(soup.findAll("tr")[i].findAll("td")[2].text)
coach.append(c)
tot.append(i)
i += 1
time.sleep(sleeptime)
emailMsg = status
subject = pnr+'-'+w
send_mail(
subject,'emailMsg', 'email-from',
[email], fail_silently=False)
lock.release()
if (status[rowlength - 1] == "CONFIRMED"):
time.sleep(sleeptime)
else:
time.sleep(1000000000000000000000000)
def fetch(request):
pnr = request.POST['pnr']
if len(pnr) != 10:
msg = "PNR must be of 10 digits ..."
return render(request, 'checkpnr.html', {'msg': msg})
email = request.POST['email']
e = request.POST['ntime']
if (e != ''):
n_time = int(e)
n = request.POST['notify']
if (n != ''):
notify = int(n)
sleeptim = notify * n_time
sleeptime= 10
# time1=request.get_all("notif")
url_pnr = "pnr url"
try:
r = requests.get(url_pnr)
data = r.text
soup = BeautifulSoup(data)
train = str(soup.find("ul", attrs={"class": "train_info"}))
train_number = soup.find("li", attrs={"class": "first"}).text
source = str(soup.find("travellers"))
route = str(soup.findAll("li")[1]).replace(
'<li>', '').replace('</li>', '')
#head, sep, tail = route.partition(' -')
travel_date = str(soup.findAll("li")[2].text)
date, sep, total = travel_date.partition('|')
rows = soup.findAll("td", attrs={"class": "pax"})
rowlength = len(rows)
chart_status = str(soup.findAll("tr")[rowlength + 1].findAll("td")[0]).replace(
'<td colspan="3"><strong>', '').replace('</strong>', '').replace('</td>', '')
passengers = []
status = []
coach = []
tot = []
w=''
i = 1
j = 1
while i <= rowlength:
j = str(soup.findAll("tr")[i].findAll(
"td")[0].text).replace(':', '')
passengers.append(j)
s = str(soup.findAll("tr")[i].findAll("td")[1].text)
w=w+','+s
status.append(s)
c = str(soup.findAll("tr")[i].findAll("td")[2].text)
coach.append(c)
tot.append(i)
i += 1
msg = "Mail not Sent"
msg1 = ''
if(email != ''):
emailMsg = status
subject = pnr+'-'+w
send_mail(
subject,'emailMsg', 'ashutosh8nitjsr@gmail.com',
[email], fail_silently=False)
msg = "mail sent.."
if __name__ == "__main__":
lock = thread.allocate_lock()
thread.start_new_thread(
check,("Thread No:1", pnr, email, sleeptime, lock))
msg1 = "thread created"
time.sleep(sleeptime)
while 1:
pass
detail2 = {
'train_number': train_number, 'route': route, 'date': date, 'chart_status': chart_status, 'tot': tot,
'passengers': passengers, 'status': status, 'coach': coach, 'msg': msg}
return render(request, 'status.html', detail2)l
except:
msg = "there was error. please try again..."
return render(request, 'checkpnr.html', {'msg': msg})
从django.http导入HttpResponse
从bs4导入BeautifulSoup
进口稀土
导入请求
从django.shortcuts导入渲染
从functools导入部分,换行
从django.core.mail导入发送邮件
导入时间
导入线程
def checkpnr(请求):
返回呈现(请求“checkpnr.html”)
def检查(字符串、pnr、休眠时间、锁、*args):
而1:
#进入临界段
lock.acquire()
#time1=请求。获取所有(“notif”)
url\u pnr=“pnr url”
r=requests.get(url\u pnr)
数据=r.text
汤=美汤(数据)
train=str(soup.find(“ul”,attrs={“class”:“train\u info”}))
train_number=soup.find(“li”,attrs={“class”:“first”}).text
source=str(soup.find(“旅行者”))
路由=str(soup.findAll(“li”)[1])。替换(
“”,“”。替换(“ ”,“”)
#head,sep,tail=route.partition('-'))
旅行日期=str(soup.findAll(“li”)[2]。文本)
日期,九月,总计=旅行日期。分区(“|”)
rows=soup.findAll(“td”,attrs={“class”:“pax”})
rowlength=len(行)
chart_status=str(soup.findAll(“tr”)[rowdength+1].findAll(“td”)[0])。替换(
“”,“”。替换(“”,“”)。替换(“”,“”)
乘客=[]
状态=[]
coach=[]
tot=[]
w=“”
i=1
j=1
而我您可以尝试在app engine上为此使用TaskQueues