syf的开发笔记-1 同时多条插入的处理

今日份的是一个关于mysql的bug

最近写了一个心跳日志的功能

大体功能是这样:

如果今天有记录就更新 没有就创建

但是出现了连续请求之后

会插入两条相同的记录 并只更新一条(因为First了)

这时候就加一个唯一索引就可以解决问题了

这样在第二次要创建的时候就不会成功

以后开发的时候也要多考虑有没有这种并发的情况出现

今天用python发post也有了一些经验

首先是要.encode("utf-8")一下 要不gin可能看不出来

然后是要加上

headers = {'Content-Type': 'application/json'}

要不也认不出来

 

测试脚本大概这个样子

 1 import json
 2 import multiprocessing
 3 import time
 4 import requests
 5 
 6 headers = {'Content-Type': 'application/json'}
 7 
 8 
 9 def post_log(start_time):
10     req = requests.Session()
11     data_to_send = json.dumps({
12         'username': 'xxxxx',
13         'password': 'xxxxx',
14         'rememberMe': True,
15     }).encode("utf-8")
16     res = req.post('xxxxxx/login', data=data_to_send)
17     print(res.text)
18 
19     log_to_send = json.dumps({
20         'xxxx':'xxxx',
21         'yyyy':'yyyy',
22     }).encode("utf-8")
23 
24     print(log_to_send)
25 
26     while True:
27         if int(time.time()) == start_time:
28             for i in range(5):
29                 res = req.post('http://xxxx', data=log_to_send, headers=headers)
30                 print(i, res.text, log_to_send)
31             break
32 
33 
34 if __name__ == '__main__':
35     now = int(time.time()) + 5
36     for i in range(4):
37         p = multiprocessing.Process(target=post_log, args=(now,))
38         p.start()

 

posted @ 2019-11-06 20:55  良将ℓ  阅读(219)  评论(0编辑  收藏  举报