我的代码:
def ws_receive(message):
text = message.content['text']
request = json.loads(text)
cmd = request['cmd']
results = run(cmd)
print(cmd)
for result in results:
Group(GROUP_NAME).send({'text':result.decode('utf-8')})
print(result)
cmd
就像ping -c 4 www.google.com
;
终端结果是:
[2017/09/24 07:24:44] WebSocket HANDSHAKING / [127.0.0.1:59285]
[2017/09/24 07:24:44] WebSocket CONNECT / [127.0.0.1:59285]
[2017/09/24 07:24:44] HTTP GET /api/hosts/?status=used 200 [0.07, 127.0.0.1:59247]
ping -c 4 www.google.com
2017-09-24 07:24:58.512885 b'PING www.google.com (74.125.23.103): 56 data bytes\n'
2017-09-24 07:24:58.513127 b'64 bytes from 74.125.23.103: icmp_seq=0 ttl=45 time=138.542 ms\n'
2017-09-24 07:24:59.517881 b'64 bytes from 74.125.23.103: icmp_seq=1 ttl=45 time=142.954 ms\n'
2017-09-24 07:25:00.522698 b'64 bytes from 74.125.23.103: icmp_seq=2 ttl=45 time=144.568 ms\n'
2017-09-24 07:25:01.562285 b'64 bytes from 74.125.23.103: icmp_seq=3 ttl=45 time=180.163 ms\n'
2017-09-24 07:25:01.562400 b'\n'
2017-09-24 07:25:01.562459 b'--- www.google.com ping statistics ---\n'
2017-09-24 07:25:01.562517 b'4 packets transmitted, 4 packets received, 0.0% packet loss\n'
2017-09-24 07:25:01.562586 b'round-trip min/avg/max/stddev = 138.542/151.557/180.163/16.662 ms\n'
但是,浏览器的websocket调试信息是:
所以,我确定频道是执行命令直到结束发送消息,但我想要实时发送消息。
答案 0 :(得分:0)
立即尝试发送:
Group(GROUP_NAME).send({'text':result.decode('utf-8')}, immediately=True)
这背后的原因是你在websocket消费者功能中。当您在其中时,Group.send
调用不会被执行,但只有在您离开函数后才会批量收集和执行。使用immediately=True
会覆盖此行为。