realpython / realpython/materials
Socket with if-loops does not give a consistent result
还没有人认领这个 Issue。
- 主要语言
- Jupyter Notebook
- 星标
- 5.2k
- 派生
- 5.3k
- 平均合并
- 4 天 10 小时
- 30 天内合并 PR
- 10
描述
The program is continuously receiving data from the sender side. And I have a checked/unchecked button when I make it checked it runs the if part...that's good. Now I make it unchecked then else part runs and prints Unchecked going to close socket, so that's also good.
The problem is when I am going to make it again checked (2nd time) to run if part again it just print Checked and my program hangs. Why is this happening? It should receive the data but it hangs. Please tell me the solution.
def show_markers(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('192.168.225.12')
s.connect((host, port))
scale=0
while True:
if self.iconAction.isChecked():
print ('Checked')
m = QgsVertexMarker(self.iface.mapCanvas())
data = s.recv(SIZE)
data1 = s.recv(SIZE)
c = data.decode()
d = data1.decode()
x = float(c)
y = float(d)
print("printing X :", x)
print("printing Y :", y)
rect = QgsRectangle(float(x)-scale, float(y)-scale, float(x)+scale, float(y)+scale)
me = self.iface.mapCanvas()
me.setExtent(rect)
me.refresh()
m.setCenter(QgsPointXY(x, y))
m.setColor(QColor(255, 0, 0))
m.setIconSize(7)
m.setIconType(QgsVertexMarker.ICON_X) # or ICON_CROSS, ICON_X
m.setPenWidth(3)
else:
print('Unchecked going to close socket')
s.shutdown(10)
s.close()
I am assuming it should work like:
- 1st time click on button ---> Checked & if part run [working]
- 2nd time click on button ---> Unchecked & else part run [working]
- 3rd time click on button ---> again it Checked [here it checked but my program hangs & not receiving the data from sender computer]
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 show_markers 入口点开始,跟踪围绕复选框状态、recv 调用以及 socket shutdown/close 路径的循环。重现三次切换的序列,然后验证返回选中状态后能够接收数据而不会挂起,并且 socket 生命周期仍然有效。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- desktop, networking
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100