0
我正在构建一个小应用程序,它会将日志尾部显示在客户端上。但是,当我在日志中添加行时,会丢失一些数据。为什么socket.io中的数据丢失?
下面是相关的Python处理器:
@socketio.on('Request logs')
def handle_request_logs():
logfile = '/path/to/some_log.log'
# gets last 25 lines from logfile
last_n_lines = tailer.tail(open(logfile), 25)
# sends the initial 25 lines
emit('Initial send', { 'lines' : last_n_lines })
# emits lines that were appended to logfile
@copy_current_request_context
def tail(logfile):
for line in tailer.follow(open(logfile)):
print("About to emit {0}".format(line))
emit('log', { 'line' : line })
# emit added lines as they come
t = threading.Thread(target=tail, args=(logfile,))
t.start()
和这里的接收'log'
的JS:
socket.on('log', function(data) {
alert("Got some data: ", data['line']);
});
每当我一些东西附加到日志(例如echo 'hello, world!' >> /path/to/some_log.log
),我就看到一个警告该客户端的消息为 "Got some data: "
。但是,我的服务器打印。
这是怎么发生的?
啊...我恨我自己。 – erip