ãªã¢ã«ã¿ã¤ã Web åå¼·ä¼ã«åå ãã
Greeããã®æé«ã§ãï¼ä¼å ´ãããã¨ããããã¾ããã
node.jsã®ãã«ã½ã³ããã£ã¦ããªã¢ã«ã¿ã¤ã Webé¢ä¿ã®LTãããã¨ããé²è¡ã®ä¼ã§ãããnode.jsã¯wktkã§ãããæãåºãã¾ããã§ããã
ãã®ãããFlaskã¨ã¿ããªå¤§å¥½ããªmeinheldã使ã£ãWebSocketã¢ããªä½ã£ã¦ã¿ã¾ããã
ãªã¢ã«ã¿ã¤ã ã§åºæ¥ããå¬ããã¨æããã¨ã«ãµã¼ãã¼ã®ç¶æ ç£è¦ãããã¾ããsshã使ããªãç°å¢ã§ãããã©ã¦ã¶ä¸ã¤ã§topããªã¢ã«ã¿ã¤ã ã«æ´æ°ããããå¬ãããªãã¨æã£ã¦ããã®ã§ãããæ°ã«ãã£ã¦ã¿ããæå¤ã¨ç°¡åã«åºæ¥ã¾ããã
ãã¡ã¤ã«æ§æ
ws_test/.
-- __init__.py -- monitor.py -- templates -- websocket.html `-- websocket_top.py
åã½ã¼ã¹
websocket_top.py
from flask import Flask, render_template, request from meinheld import server, middleware from multiprocessing import Process from subprocess import call import commands, time SECRET_KEY = 'development key' DEBUG=True app = Flask(__name__) app.config.from_object(__name__) participants = set() @app.route('/') def index(): return render_template('websocket.html') @app.route('/top') def top(): ws = request.environ.get('wsgi.websocket') participants.add(ws) try: while True: print "ws.wait()..." m = ws.wait() print "recv msg %s" % m if m is None: break for p in participants: print "send message %s" % m if m.isdigit(): mp = Process(target=send_top, args=(p, int(m))) mp.start() finally: participants.remove(ws) return "" def send_top(obj, count=5): for i in range(count): str_top = ["%s"%x for x in commands.getoutput("top -b -n1").split('\n')[:10]] res_str = "<pre>" + '\n'.join(str_top) + '</pre>' obj.send(res_str) if __name__ == "__main__": server.listen(("0.0.0.0", 5000)) server.run(middleware.WebSocketMiddleware(app))
websocket.html
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script> window.onload = function() { var data = {}; var s = new WebSocket("ws://main.atusi.me:5000/top"); s.onopen = function() { s.send('New participant joined'); }; s.onmessage = function(e) { $("#top-stat").html("<div>" + e.data + "</div>"); }; s.onclose = function(e) { $("#top-stat").html("<div>" + 'close connection' + "</div>"); } $('#connect').submit(function (evt) { var line = $('#connect [type=text]').val() $('#connect [type=text]').val(line) s.send(line); return false; }); $('#disconnect').submit(function (evt) { s.onclose(); return false; }); }; </script> <style> #chat { font:12px "DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace; } </style> </head> <body> <h3>top</h3> <form id="connect"> <input type="text" value=3 /> <input type="submit" value='open' /> </form> <form id="disconnect"> <input type="submit" value='close' /> </form> <div id="top-stat" style="width: 60em; height: 20em; overflow:auto;"> </div> </body> </html>
ã¯ã¾ãã©ã
Pythonããtopã®å 容ãæããªã
callã§topå¼ãã§ãã¾ãã¨ãæ¨æºåºåããã¦ãã¾ããè¿ã£ã¦ããªãã®ã§å°ãã¾ãã
topã«é¢ãã¦ã¯ãtop -b -n1 ãªãã·ã§ã³ä»ãããã¨ã§ä¸åã ãå®è¡ãã¦è¿ã£ã¦ããããã«ãªã£ãã®ã§è§£æ±ºï¼thx i386æ°
callã¯ãã¨ãã¨æ¬²ããæ©è½ãã¤ãã¦ãªãã½ãã®ã§ï¼2.7ããããããäºãåºæ¥ãããã«ãªã£ã¦ãï¼ãcommands.getoutput ã使ããã¨ã§æ¨æºåºåé¨åãåå¾ãããã¨ãåºæ¥ã¾ããã
è¤æ°åtopããã¨ãä»ã®é²è¦§è ã®äººããããã¯ããã¦ãã¾ã
ä¸åã ãã§å®è¡ããã¨ãè¤æ°ã®é²è¦§è
ãããå ´åã«ããããã®ãã©ã¦ã¶ã§äº¤äºã«æ´æ°ãè¡ããã¦ããããã¤åä½ããããã ãã©ãè¤æ°åã®topãå®è¡ããã¨ä¸äººã«å¯¾ãã¦è¡¨ç¤ºãæ´æ°ããæå®ã®åæ°ãçµäºããã¾ã§ããã®ä»ã®äººã®ãã©ã¦ã¶ã¯ä¸åæ´æ°ããã¾ãããããã ã¨åæã«è¤æ°ã¶æããã®ã¢ãã¿ã¼ãåºæ¥ãªãçºNGã
ã©ã®ããã«è§£æ±ºãã¦ãããæªã ãã¹ãããããã¾ããããä»åã¯ãwsãªãã¸ã§ã¯ãããã®ã¾ã¾å¼æ°ã§æ¸¡ãå½¢ã§multiprocessingã§å¥ããã»ã¹åãã¦ããããã³ã°ãåé¿ãã¾ããããã©ã¼ã ã«ç¹°ãè¿ãåæ°ãæ°å¤å
¥åããã¨ãã®åæ°ã ãtopãç¹°ãè¿ãã¾ãã
ãã£ã¨æ£ããããæ¹ãããã¨ããããã ãã©æãã¤ããªãã§ããããªã¼ãºãã«ããã£ã§ãã
JSã®é¨åãªãã ãã©ãWebSocketã®closeã®ä»æ¹ãåãããªããã©ãããã°ãããã ãï¼
ãªã¢ã«ã¿ã¤ã ã«ã¤ãã¦ã®å¦æ³
âãªã¢ã«ã¿ã¤ã ãªãã¨ãâã¨è¨ãã®ãæè¿ããè¦ãããããã«ãªã£ããã©ããªã¢ã«ã¿ã¤ã ã®å®ç¾©ã£ã¦ãªãã ããï¼ã¨ã¡ãã£ã¨èãããããããä½é 延ã¨ããåºå®é 延ã¨ããããããæå³ã§âãªã¢ã«ã¿ã¤ã âã¨ããè¨èãWebçéã§ä½¿ããã¦ããæ°ããããç¾å®æéã¯Noté¢æ£ãªã®ã§ãPCã§æ±ããã¨ããã¨ä½ãããé¢æ£ãããªãã¦ã¯ãããªãã¨ãªãã¨ããã®å解è½ãã©ããããã«ããã®ãã«èå³ã湧ãï¼æ ç»ã ã£ãã24F/secã ãããã¬ãã ã£ãã60i/secã ããé³æ¥½ãã¡ã¤ã«ã ã£ãã44.1KHzã ããCPUã¨ãã ã¨2.5GHzã¨ãï¼å¤å人éã®è¦è¦çã«ã¯250Update/Sec以ä¸ã¯èªç¥ä¸è½ã ã¨æãã®ã§ãããããã®å解è½ãåºæºãã¦ã¿ãã®ã¯ã©ããªãã ããï¼ã§ãå®éã®ã¨ããã ç»é¢ã®ãã¼ã¿ã¯ãªã¢ã«ã¿ã¤ã ã§æ´æ°ï¼250Update/Sec)ãã¦ããã¤ããã§ãã液æ¶ã¢ãã¿ã¼ããããã60Hzã ã¨ãã¾ãæå³ããªããããããã¯ã¼ã¯ã®è¡ã£ã¦æ¥ãã§25msãããã£ã¦ãã40å/secãªãã(éåæã«ããã°ãªãã¼ã§ãè¡ãããï¼)ã§250ã£ã¦ããæ°åã¯ãã¾ãç¾å®çãããªãã®ããªï¼æè¿ã®å°ãã¸ãã¬ãã§4åéã¨ãã¯240F/secç¸å½ãªã®ã§ããã®ã¡ãã£ã¨ä¸ãããã®æ°åã§PCãªã¢ã«ã¿ã¤ã ã®åºæºæ°åãä½ã£ãã»ãããããã¨æããã ãã©ãã©ããªãã ããï¼
ã¨ããå¦æ³ãããããåå¼·ä¼ã§ããã
æ©ãã ããã¿ããªã³ãçºè¦ãã¦ãã ããã