修复了数据库连接不断开的bug

This commit is contained in:
MarkusJoe
2022-02-09 16:43:55 +08:00
parent baf99a905c
commit a1672a4294
5 files changed
+76 -145

No files matched your search

+9
View File
@@ -16,6 +16,15 @@
## 部署
> 由于对`vercel`不熟悉, vercel的配置文件搞不明白所以暂时没有vercel的配置文件, 我也试了很多方法去部署但是要么是500要么是400
> 在部署之前你需要先安装`python3.9.x`以上的版本
### 部署到本地服务器
```shell
$ git clone https://github.com/MarkusJoe/RequestCounter.git
$ cd RequestCounter
$ pip3 install -r requirements.txt
$ python3 app.py
```
## 调用须知
- API支持`GET` 和 `POST` 方法请求
- 最大可以计数`10`位数, 超过则重置
+7 -2
View File
@@ -7,7 +7,12 @@
import requests
import threading
for i in range(100):
print(requests.get('http://127.0.0.1:5000/API?name=89999&length=10&theme=t2').elapsed.microseconds)
def thread_():
print(requests.get('http://127.0.0.1:5000/get?name=89999&length=10&theme=t2').elapsed.microseconds)
for i in range(1000000):
threading.Thread(target=thread_).start()
BIN
View File
Binary file not shown.
+48 -32
View File
@@ -18,8 +18,14 @@ def insert_data(name: str) -> None:
:param name:
:return:
"""
cursor.execute('insert into ReqCount values(?, ?)', (name, 1))
conn.commit()
conn = sqlite3.connect('./db/data.db', check_same_thread=False)
cursor = conn.cursor()
try:
cursor.execute('insert into ReqCount values(?, ?)', (name, 1))
conn.commit()
finally:
cursor.close()
conn.close()
def fetch_data(name: str) -> int:
@@ -28,26 +34,30 @@ def fetch_data(name: str) -> int:
:param name:
:return:
"""
cursor.execute('select * from ReqCount')
conn.commit()
data = cursor.fetchall()
conn = sqlite3.connect('./db/data.db', check_same_thread=False)
cursor = conn.cursor()
try:
cursor.execute('select * from ReqCount')
conn.commit()
data = cursor.fetchall()
# 将返回的元组转换为字典
temp_dict = {}
for k, v in data:
temp_dict.setdefault(k, []).append(v)
# 上方代码将元组转换为字典后字典的值会变成只有一个值的列表, 用下面两行代码去除
for i, c in zip(temp_dict.keys(), temp_dict.values()):
temp_dict[i] = c[0]
temp_dict = {}
for k, v in data:
temp_dict.setdefault(k, []).append(v)
for i, c in zip(temp_dict.keys(), temp_dict.values()):
temp_dict[i] = c[0]
if name in temp_dict.keys():
count = temp_dict[name] # 获取原数字 为 整型
update_data(name, count)
return count
else:
# 新建用户数据
insert_data(name)
return 0
if name in temp_dict.keys():
count = temp_dict[name] # 获取原数字 为 整型
update_data(name, count)
return count
else:
# 新建用户数据
insert_data(name)
return 0
finally:
cursor.close()
conn.close()
def update_data(name: str, times: int) -> None:
@@ -57,9 +67,15 @@ def update_data(name: str, times: int) -> None:
:param times:
:return:
"""
times += 1
cursor.execute('update ReqCount set times=? where name=?', (times, name))
conn.commit()
conn = sqlite3.connect('./db/data.db', check_same_thread=False)
cursor = conn.cursor()
try:
times += 1
cursor.execute('update ReqCount set times=? where name=?', (times, name))
conn.commit()
finally:
cursor.close()
conn.close()
def fetch_table() -> list:
@@ -67,15 +83,15 @@ def fetch_table() -> list:
返回已有主题列表
:return:
"""
lst = []
conn_temp = sqlite3.connect('./bin/assets/theme.db', check_same_thread=False)
cursor_temp = conn_temp.cursor()
cursor_temp.execute("select * from sqlite_master where type='table'")
for i in cursor_temp.fetchall():
lst.append(i[1])
return lst
try:
lst = []
cursor_temp.execute("select * from sqlite_master where type='table'")
for i in cursor_temp.fetchall():
lst.append(i[1])
return lst
finally:
cursor_temp.close()
conn_temp.close()
if __name__ != '__main__':
conn = sqlite3.connect('./db/data.db', check_same_thread=False)
cursor = conn.cursor()
+12 -111
View File
@@ -37,119 +37,20 @@
测试用代码如下
```python
import requests
import threading
for i in range(100):
res = requests.get('http://127.0.0.1/API?name=2&length=10&theme=t2').elapsed.microseconds
print(res) # 返回响应速度 单位: 微秒
def thread_():
print(requests.get('http://127.0.0.1:5000/get?name=89999&length=10&theme=t2').elapsed.microseconds)
for i in range(1000000):
threading.Thread(target=thread_).start()
```
>在`AMD Athlon(tm) X4 830 Quad Core Processor 2.99 GHz` `4G` 内存的电脑上运行项目
> 在本机上使用`Requests`库`GET`方法请求`100`次 (名称为`2`长度为`10`主题为`t2`)
> 使用`elapsed.microseconds`属性 获取响应速度返回的值如下
<details>
单位: 微秒<br>
参考: 1秒(second)(s) = 1000毫秒(millisecond)(ms) = 1000000微秒(microseconds)(us)<br>
61764<br>
22125<br>
25059<br>
16524<br>
13396<br>
12787<br>
20579<br>
18987<br>
25500<br>
14910<br>
11303<br>
11787<br>
16949<br>
17094<br>
15264<br>
10934<br>
10526<br>
11943<br>
16967<br>
17205<br>
12887<br>
11386<br>
11547<br>
17743<br>
18625<br>
17208<br>
14763<br>
12858<br>
12708<br>
18598<br>
16879<br>
16216<br>
14655<br>
12089<br>
11588<br>
17287<br>
20115<br>
14403<br>
12857<br>
11596<br>
15006<br>
18246<br>
18013<br>
13623<br>
10673<br>
12064<br>
17838<br>
16651<br>
16483<br>
13522<br>
13231<br>
15624<br>
11043<br>
10765<br>
16317<br>
17200<br>
13250<br>
11230<br>
11533<br>
16727<br>
16781<br>
16253<br>
14658<br>
13762<br>
13194<br>
17700<br>
17933<br>
15686<br>
12051<br>
10904<br>
17163<br>
16591<br>
16464<br>
15271<br>
12405<br>
17173<br>
26737<br>
15964<br>
12363<br>
12653<br>
12893<br>
17524<br>
20084<br>
14165<br>
12037<br>
13427<br>
17245<br>
18652<br>
18438<br>
14811<br>
14233<br>
14555<br>
17998<br>
16949<br>
16445<br>
12120<br>
11695<br>
14257<br>
17557<br>
16587<br>
</details>
<br>
>在`AMD Athlon(tm) X4 830 Quad Core Processor 2.99 GHz` `4G` 内存的电脑上运行项目<br>
> 在本机上使用`Requests`库`GET`方法请求`1000000`次 (名称为`2`长度为`10`主题为`t2`)<br>
> 使用`elapsed.microseconds`属性 获取响应速度在 `0.03` ~ `0.8` 秒左右
# 关于
## 开源