使用python框架实现多线程
python和线程是新手,所以我不确定线程是否正在发生,如下所述-- 在这里,我尝试为每个事件部署一个单独的线程。当用户每次按下“提交按钮”时,应该创建一个新线程,然后执行它 a.py文件:使用python框架实现多线程,python,python-3.x,multithreading,python-multithreading,Python,Python 3.x,Multithreading,Python Multithreading,python和线程是新手,所以我不确定线程是否正在发生,如下所述-- 在这里,我尝试为每个事件部署一个单独的线程。当用户每次按下“提交按钮”时,应该创建一个新线程,然后执行它 a.py文件: from bottle import request, template,route,run,get,post import sqlite3 import threading import datetime @route('/') def index(): return template('ins
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')
<!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>
<!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:
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')
<!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>
<!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>
选择一个时间:
A.
B
C
结果。第三方物流:
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')
<!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>
<!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>
%对于键,result.items()中的值:
{{key}}
{{value}}
%结束
ins.tpl和result.tpl都存储在views文件夹中(“因为我使用的是瓶子”)。我不确定是否每次都生成一个新线程。还是有更好的方法 代码看起来确实在每次提交时启动了一个新线程。这很容易使用验证
calc()中似乎存在争用条件,多个线程通过print()函数竞争访问标准输出。您可以通过将所有输出行合并为一个字符串并一次性打印(或者更好,创建一个打印队列并在单独的线程中进行打印)来解决此问题。代码看起来确实会在每次提交时启动一个新线程。这很容易使用验证
calc()中似乎存在争用条件,多个线程通过print()函数竞争访问标准输出。您可以通过将所有输出行合并为一个字符串并一次性打印(或者更好,创建一个打印队列并在单独的线程中进行打印)来解决此问题。此外,还有线程、多处理、队列,还有池。我有点不知道该用什么/在哪里。在我进入线程/处理之前,我可以了解一些信息吗?@captainalooreptin'这里有一个关于并发方法的视频链接,这里有幻灯片,还有线程、多处理、队列,还有池。我有点不知道该用什么/在哪里。在我进入线程/处理之前,我可以了解一些信息吗?@captainalooreptin'这里是一个关于并发方法的视频链接,下面是幻灯片,