python - 运行时错误无法启动新线程
问题描述
我正在尝试检查一个端口上是否有可用的数据。如果这是可用的,则应将消息“是”发送到另一个端口,如果没有数据,则应发送“否”消息。客户端正在连接到“是”或“否”即将到来的那个端口。我运行脚本,一切看起来都很好。但是一小时后我得到了错误:运行时错误无法启动新线程。线程 11 中的异常:Traceback< 最近一次调用:文件 threading.py 引导带内部的第 914 行。
我是 python 新手,我真的不明白发生了什么。我的代码包含许多线程,因为我正在检查来自 10 个端口的数据并向其他 10 个端口发送“是”或“否”消息。这是我的 1 端口代码的一部分:
import time
import socket
import threading
from threading import Thread
import select
#-----------------------------------Server --------------------------
s = socket.socket(socket.AF_INET) #Socket
h = '127.0.0.1' #Host where data coming from
p = 14201 #Port
halarm = "127.0.0.1" # A port will be opened to send availabilty status
palarm = 14202 # Port
def Server ():
while True:
try:
time.sleep(0.1)
s.connect((h, p))
except socket.error:
pass
#-----------------------------------Server/Client -------------------------------
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((halarm, palarm)) # Bind to the Port where
sock.listen(1)
def func(conn): # Server-Client Function
while True:
try:
global s
Timeout = select.select([s], [], [], 5)# Timeout in seconds
connected = True
while True:
try:
if Timeout[0]:
conn.send(bytes("yes", "utf-8"))
time.sleep(3)
else:
conn.send(bytes("no", "utf-8"))
time.sleep(3)
newConnection= False
while not newConnection:
try:
s = socket.socket(socket.AF_INET)
s.connect((h, p))
Timeout = select.select([s], [], [], 5)# Timeout in seconds
newConnection= True
except socket.error:
conn.send(bytes("Port is closed", "utf-8"))
time.sleep(3)
pass
except socket.error:
pass
except:
pass
def connThread():
while True:
conn, addr = sock.accept()
time.sleep(0.1)
Thread(target = func, args=(conn,)).start()
if __name__ == '__main__':
Thread(target = Server).start()
Thread(target = connThread).start()
我怎么解决这个问题 ?我感谢您的所有帮助。
解决方案
推荐阅读
- python - 如何从另一个 .py 脚本中的 .py 脚本访问变量
- java - 编辑器窗口中的工件:错误的 Eclipse 配置或错误?
- r - 使用for循环对数据框中的列进行求和,创建新的数据框
- java - 尝试使用服务帐户凭据创建代码以将 JSON 发送到 GCP 时出错
- neo4j - 所有孩子的 Neo4j 循环
- react-native-navigation - 如何使用本机反应在菜单页面中获取导航参数
- tcp - 了解 Wireshark 中捕获的 TCP 会话
- .net - 如何通过 .net 代码读取扩展事件
- windows - 任务调度程序 - 任务注册被删除
- delphi - SysUtils.LoadPackage 替换 Aplication.OnMessage 处理程序