ログを追跡してクライアントに表示する小さなアプリを構築しています。ただし、ログに行を追加すると、一部のデータが失われます。
関連する 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()
を受け取る JS は次の'log'
とおりです。
socket.on('log', function(data) {
alert("Got some data: ", data['line']);
});
ログに何かを追加するたびに (たとえばecho 'hello, world!' >> /path/to/some_log.log
)、クライアントに警告が表示され、メッセージが表示されます
"Got some data: "
。ただし、私のサーバーは"About to emit hello, world!"
.
なぜこうなった?