简体   繁体   English

套接字连接在3次迭代后中止-python

[英]Socket connection aborted after 3 iterations - python

I'm developing an application where I want to stream an audio stream to a web page. 我正在开发一个应用程序,我想将音频流传输到网页上。 When I open a connection and send a message, the server and client receive the message (s.send('Hi')) . 当我打开连接并发送消息时,服务器和客户端会收到消息(s.send('Hi')) But when I start a loop, my script stops after exact 3 iterations and then quits. 但是,当我开始循环时,我的脚本会在经过3次精确的迭代后停止,然后退出。

I noticed that the server throws an creating default object from empty value error after the first iteration 我注意到服务器在第一次迭代后creating default object from empty value错误抛出了一个creating default object from empty value

import websocket
# https://github.com/liris/websocket-client

HOST = 'ws://127.0.0.1:8080'

s = websocket.create_connection(HOST)

s.send('Hii')

i = 0
while 1:
    i += 1
    print(i)
    s.send(str(i))
    time.sleep(0.5)

This is the full error traceback 这是完整的错误回溯

1
2
3
Traceback (most recent call last):
  File "E:\Redux\Win32\streamingdata.py", line 20, in <module>
    s.send(str(i))
  File "C:\Python33\lib\site-packages\websocket\__init__.py", line 656, in send
    return self.send_frame(frame)
  File "C:\Python33\lib\site-packages\websocket\__init__.py", line 680, in send_frame
    l = self._send(data)
  File "C:\Python33\lib\site-packages\websocket\__init__.py", line 900, in _send
    return self.sock.send(data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

Where does it go wrong? 哪里出错了? Using pure sockets to a remote listener, the script executes and the server receives the messages. 使用纯套接字连接到远程侦听器,脚本将执行,服务器将接收消息。 Using websocket it fails. 使用websocket会失败。 Someone who can help me out? 有人可以帮助我吗?

Try replacing your time.sleep(0.5) by gevent.sleep(0.5) 尝试更换您的time.sleep(0.5)gevent.sleep(0.5)

As websocket is based on gevent , you shall prevent using blocking operations. 由于websocket基于gevent ,因此您应避免使用阻止操作。

As the time.sleep is blocking one, you are likely blocking sending the data by webscocket , which has no chance to communicate during your sleep. 由于time.sleep挡住一个,你很可能阻止通过发送数据webscocket ,其中有没有机会你的睡眠过程中进行交流。

gevent.sleep shall give gevent greenlet a chance to live. gevent.sleep将使gevent greenlet有生存的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM