添加互斥锁
This commit is contained in:
@@ -38,7 +38,7 @@ LOCAL_SERVER_IP = ("192.168.1.40",22)
|
||||
|
||||
|
||||
|
||||
|
||||
_local_client_lock = threading.Lock()
|
||||
|
||||
|
||||
# 发送数据到指定ip地址和端口
|
||||
@@ -51,6 +51,8 @@ def send_to(ip,port,data:bytearray):
|
||||
# 关闭指定地址的端口
|
||||
def close(ip,port):
|
||||
global _local_client
|
||||
global _local_client_lock
|
||||
_local_client_lock.acquire()
|
||||
for item in _local_client:
|
||||
if(item[1]==ip and item[2]==port):
|
||||
myprint(f"断开连接 {ip}:{port}")
|
||||
@@ -58,14 +60,18 @@ def close(ip,port):
|
||||
# 删除已被关闭的条目
|
||||
_local_client.remove(item)
|
||||
break
|
||||
_local_client_lock.release()
|
||||
|
||||
# 关闭所有
|
||||
def close_all():
|
||||
global _local_client
|
||||
global _local_client_lock
|
||||
_local_client_lock.acquire()
|
||||
for item in _local_client:
|
||||
item[0].close()
|
||||
# 关闭端口之后把列表置空
|
||||
_local_client=[]
|
||||
_local_client_lock.release()
|
||||
myprint('连接列表已清空')
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user