diff options
| -rw-r--r-- | apps/delay.py | 77 |
1 files changed, 57 insertions, 20 deletions
diff --git a/apps/delay.py b/apps/delay.py index 67b4fd8..9e0c0e7 100644 --- a/apps/delay.py +++ b/apps/delay.py @@ -1,5 +1,6 @@ # 时延测试接口 import random +import threading import requests from apiflask.validators import OneOf @@ -52,29 +53,65 @@ def get_pernode_delay(query_data,type): if 'ip' in query_data.keys(): addr=query_data['ip'] ans = [] + threads=[] + df = pd.read_csv("./server.csv", encoding="utf-8") + for index, row in df.iterrows(): + mythread=threading.Thread(target=task,args=[ans,addr,row,type]) + mythread.start() + threads.append(mythread) + for t in threads: + t.join() + # if type=="icmp": + # df=pd.read_csv("./server.csv",encoding="utf-8") + # for index,row in df.iterrows(): + # ans.append({ + # 'Id':row['id'], + # 'CurrDelay':icmp_delay_query(addr,row['ip']), + # 'Type':type}) + # if type=="tcp": + # df=pd.read_csv("./server.csv",encoding="utf-8") + # for index,row in df.iterrows(): + # ans.append({ + # 'Id':row['id'], + # 'CurrDelay':tcp_delay_query(addr,row['ip']), + # 'Type':type}) + # if type=="dns": + # df=pd.read_csv("./server.csv",encoding="utf-8") + # for index,row in df.iterrows(): + # ans.append({ + # 'Id':row['id'], + # 'CurrDelay':dns_delay_query(addr,row['ip']), + # 'Type':type}) + return {'delay_data':ans} + +threadLock = threading.Lock() +def task(ans,addr,row,type): if type=="icmp": - df=pd.read_csv("./server.csv",encoding="utf-8") - for index,row in df.iterrows(): - ans.append({ - 'Id':row['id'], - 'CurrDelay':icmp_delay_query(addr,row['ip']), - 'Type':type}) + res=icmp_delay_query(addr,row['ip']) + threadLock.acquire() + ans.append({ + 'Id':row['id'], + 'CurrDelay':res, + 'Type':type}) + threadLock.release() + return if type=="tcp": - df=pd.read_csv("./server.csv",encoding="utf-8") - for index,row in df.iterrows(): - ans.append({ - 'Id':row['id'], - 'CurrDelay':tcp_delay_query(addr,row['ip']), - 'Type':type}) + res=tcp_delay_query(addr, row['ip']) + threadLock.acquire() + ans.append({ + 'Id':row['id'], + 'CurrDelay':res, + 'Type':type}) + threadLock.release() + return if type=="dns": - df=pd.read_csv("./server.csv",encoding="utf-8") - for index,row in df.iterrows(): - ans.append({ - 'Id':row['id'], - 'CurrDelay':dns_delay_query(addr,row['ip']), - 'Type':type}) - return {'delay_data':ans} - + res=dns_delay_query(addr, row['ip']) + threadLock.acquire() + ans.append({ + 'Id':row['id'], + 'CurrDelay':res, + 'Type':type}) + threadLock.release() def icmp_delay_query(target,addr): try: |
