Новичок в python и потоке, поэтому я не уверен, что потоки происходят, как указано ниже -
Здесь я пытаюсь развернуть отдельный поток для каждого события.Новый поток должен быть создан, когда пользователь каждый раз нажимает «кнопку отправки», а затем запускает его.
Файл a.py:
from bottle import request, template,route,run,get,post
import sqlite3
import threading
import datetime
@route('/')
def index():
return template('ins')
@post('/result')
def result():
# print(request.body.read()) gives raw data
result = request.forms
usr_time = request.forms['usr_time'] #get all the values using keys
A = request.forms.get('A')
B = request.forms.get('B')
C = request.forms.get('C')
usr_hour,usr_mins = usr_time.split(":")
with sqlite3.connect("database.db") as conn:
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS bottable(hour TEXT, minutes TEXT, A TEXT, B TEXT, C TEXT)")
cur.execute("INSERT INTO bottable(hour,minutes,A,B,C) VALUES (?,?,?,?,?)", (usr_hour,usr_mins,A,B,C))
cur.execute("select * from bottable")
data = cur.fetchall() #get the whole table
conn.commit()
t1=threading.Thread(target=calc, args=(data,))
t1.start()
return template("result",result = result)
def calc(data):
print(data) #prints the whole table
match_not_found=True
while match_not_found:
h=datetime.datetime.today().strftime("%H")
mi=datetime.datetime.today().strftime("%M")
# z=[(i[2],i[3],i[4]) for i in data if i[0] == h and i[1]==mi]
for i in data:
if i[0] == h and i[1]==mi:
print ([j for j in i[2:5] if j != None])
match_not_found=False
break
if __name__ == '__main__':
run(host='localhost',port=8080,debug='True',reloader='True')
ins.tpl:
<!DOCTYPE html>
<html>
<body>
<form action="http://localhost:8080/result" method = "POST">
Select a time:
<input type="time" name="usr_time">
<br> <br>
<input type="checkbox" name="A" value="A is on" >A </input>
<br>
<input type="checkbox" name="B" value="B is on" >B </input>
<br>
<input type="checkbox" name="C" value="C is on" >C </input>
<br><br>
<input type="submit"> </input>
</form>
</body>
</html>
result.tpl:
<!doctype html>
<html>
<body>
<table border = 1>
%for key, value in result.items():
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
%end
</table>
</body>
</html>
И ins.tpl, и result.tpl хранятся в папке представлений (потому чтоя использую бутылку).Я не уверен, что каждый раз генерируется новый поток.Или есть лучший способ сделать это?