socket.io python big file Transmission problem. help me,please.

问题描述

我使用socket.io向客户端发送文件数据,启动一个发送线程后,emit()对应的接收事件只会接收一次,但发送还是正常的。
I send big file data to the client using socket.io, and response(event named "file_content" ) of emit() only once after in a send thread that I defined it myself .

相关代码 The relevant code

@sio.on("file_content")

def send_fileContent(sid, temp):

#我将sio和session都传给新的线程

#I passed the variables sio and session to the new thread

threading.Thread(

target=send_fileContent_thread, args=(sio, handle_dir, session,)).start()

线程的Run方法
The Run method of a thread

def send_fileContent_thread(sio_re, handle_dir, session):

"""

发送文件内容的线程

"""

with open(handle_dir, "r", encoding='utf-8') as f:

# if the code is `for i in range(10):`it has the same result.

# 如果for i in range(10):也是只接收一次,发送都没问提

while True:

line = f.read(10)

print(line)

if not line:

break

sio_re.emit('file_content', line)

print("test 发送了数据")

time.sleep(3)

但这样客户端file_content事件只接收到了一次.
However, the client event named file_content is only received once.
但以下这样就可以接收到多次
But the following can be received multiple times

def send_fileContent_thread(sio_re, handle_dir, session):

"""

发送文件内容的线程

The thread that sends the contents of the file

"""

for i in range(10):

sio_re.emit('file_content', i)

客户端代码如下:
The client code is as follows:
q是一个队列,q = queue.Queue(),用于缓存数据
q is a queue, q = queue.queue (), used to cache data

@sio.on('file_content')

def file_content(data):

"""

接收文件内容

Receive file contents

"""

print("test", data)

q.put(data)

这是我本地运行的结果
This is the result of my local computer
socket.io python big file Transmission problem. help me,please.


回答:

我补了解socket.io,但从你的代码看来,问题很可能出在time.sleep
参照下sio的文档:https://python-socketio.readt...
不知道你是不是用background方式跑起来的,也许你该试试sio.sleep


回答:

is there anyone help me?

找到原因了。

原因是因为底层的eventlet是使用的协程
socket.io python big file Transmission problem. help me,please.
协程即这里的greenthread。
而我如果使用time.sleep()相当于休息整个python thread。
比如我greenthread1向客户端发送data1(但还未完全发送),我此时调用time.sleep(),整个python thread进入休息。休息完成后greenthread2开始发送data2(但还未完全发送),又休息,直到time.sleep()运行完后,所有greenthread进行后半部分的发送。
如下代码:

Server.py

import eventlet

import time

sockets = {}

client_f = ""

def handle():

while True:

if not client_f:

continue

for i in range(10):

sockets[client_f].send(bytes(str(i), encoding='utf-8'))

server = eventlet.listen(('127.0.0.1', 9999))

pool = eventlet.GreenPool()

while True:

try:

client_sock, client_addr = server.accept()

sockets[client_addr] = client_sock

client_f = client_addr

print("accepted", client_addr)

pool.spawn_n(handle)

for i in range(3):

#这种休眠方式会一直阻塞整个pythonthread,从而什么也发不出去

time.sleep(2)

#这种瞬间就发出去了。

# eventlet.sleep(2)

print(time.strftime('%H: %M: %S',

time.localtime(time.time())), "I'm sleeping")

except (SystemExit, KeyboardInterrupt):

break

Client.py

import eventlet

import time

socket = eventlet.connect(('127.0.0.1', 9999))

for i in range(5):

t = socket.recv(1000).decode()

if t:

print(time.strftime('%H: %M: %S',

time.localtime(time.time())), t)

以上是 socket.io python big file Transmission problem. help me,please. 的全部内容, 来源链接: utcz.com/p/937681.html

回到顶部