{"id":439053,"date":"2021-04-12T21:42:16","date_gmt":"2021-04-12T13:42:16","guid":{"rendered":"http:\/\/4563.org\/?p=439053"},"modified":"2021-04-12T21:42:16","modified_gmt":"2021-04-12T13:42:16","slug":"flask-socketio-%e5%a4%9a%e7%ba%bf%e7%a8%8b%e9%81%87%e5%88%b0%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=439053","title":{"rendered":"Flask-SocketIO \u591a\u7ebf\u7a0b\u9047\u5230\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Flask-SocketIO \u591a\u7ebf\u7a0b\u9047\u5230\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Alexf4 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6700\u8fd1\u5728\u4f7f\u7528 Flask-SocketIO \u60f3\u5b9e\u73b0\u5b9e\u65f6\u8ddf\u8e2a <code>celery<\/code> \u7684\u4efb\u52a1\u65e5\u5fd7\uff08\u6bcf\u4e2a\u4efb\u52a1\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff09\u7684\u529f\u80fd\u3002 \u5728\u672c\u5730\u8fd0\u884c\uff0c\u5f00\u4e86\u4e24\u4e2a\u7a97\u53e3\uff0c\u7b2c\u4e8c\u4e2a\u7a97\u53e3\u4f1a\u8f93\u51fa\u81ea\u5df1\u7684\u65e5\u5fd7\uff0c\u7b2c\u4e00\u4e2a\u7a97\u53e3\u4f1a\u8f93\u5165\u81ea\u5df1\u7684\u52a0\u540e\u4e00\u4e2a\u7a97\u53e3\u7684\u65e5\u5fd7\u3002\u7c7b\u4f3c\u540e\u7eed\u6bcf\u5f00\u7684\u4e00\u4e2a\u7a97\u53e3\uff0c\u524d\u9762\u7684\u7a97\u53e3\u90fd\u4f1a <code>append<\/code> \u6b64\u7a97\u53e3\u7684\u65e5\u5fd7\u3002<\/p>\n<p>client<\/p>\n<pre><code>import sys  import socketio   client = socketio.Client(reconnection=False)   @client.on('log', namespace='\/cdc') def on_log(args):     print(args['data'])   def send(log_path):     try:         client.connect('http:\/\/127.0.0.1:5000', namespaces='\/cdc')         client.emit('log', {'log_path': log_path}, namespace='\/cdc')     except KeyboardInterrupt:         try:             sys.exit(0)         except SystemExit:             sys.exit(1) <\/code><\/pre>\n<p>server<\/p>\n<pre><code>sio = flask_socketio.SocketIO(async_mode=async_mode)   @sio.on('log', namespace='\/cdc') def log(data):     thread = sio.start_background_task(scan_log, data['log_path'], None, flask.request.sid)   def scan_log(log_path, app=None, client_id=None):     counter = 0      with open(log_path, 'r') as f:         while True:             line = f.readline()             if not line:                 sio.sleep(0.1)                 continue             counter += 1             sio.emit('log', {'data': line.strip()}, namespace='\/cdc', to=client_id) <\/code><\/pre>\n<p>\u5404\u4f4d\u6709\u6ca1\u6709\u9047\u5230\u7c7b\u4f3c\u7684\u60c5\u51b5\uff1f\u6216\u8005\u6709\u4ec0\u4e48\u5927\u6982\u7684\u601d\u8def\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5753324\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dassh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6bcf\u4e2a\u65e5\u5fd7\u5e94\u8be5\u6709\u4e00\u4e2a\u72ec\u7acb\u7684\u6807\u8bc6\uff0c\u7136\u540e on_log \u91cc\u5bf9\u6807\u8bc6\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u662f\u5bf9\u5e94\u7684\u624d print                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5753325\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Alexf4 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dassh \u8fd9\u91cc\u6bcf\u4e00\u4e2a\u65e5\u5fd7\u90fd\u662f\u72ec\u7acb\u7684\uff08\u6bcf\u4e2a\u4efb\u52a1\u90fd\u751f\u6210\u5bf9\u5e94\u7684\u65e5\u5fd7\uff09<br \/>\u6240\u4ee5 client on log \u4f1a\u63a5\u53d7 server \u591a\u4e2a\u7ebf\u7a0b\u5206\u522b emit \u7684\u6570\u636e\uff1f<br \/>\u6211\u8fd9\u4e2a scan log \u4e0d\u662f\u7528\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u7684\u4e48\uff0c\u53e6\u5916 emit \u7684\u65f6\u5019\u7528 to \u8fd9\u4e2a\u53c2\u6570\u4e5f\u533a\u522b\u4e86 client \u7684\u53d1\u9001\u7684\u5440                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5753326\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : dassh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sio.on(&#8216;log&#8217;, &#8230;)\u5c31\u8868\u793a\u4f60\u8ba2\u9605\u4e86\u6240\u6709 server emit \u7684&#8217;log&#8217;\u7684\u4e8b\u4ef6\uff0c\u6240\u4ee5\u4f60\u9700\u8981\u8fdb\u884c\u8fc7\u6ee4\u5224\u65ad\uff0cto \u53c2\u6570\u6211\u6ca1\u7528\u8fc7\u54c8\uff0c\u7ed9\u4f60\u4e00\u79cd\u89e3\u51b3\u65b9\u6848<br \/>&#8220;`<\/p>\n<p>@client.on(&#8216;log&#8217;, namespace=&#8217;\/cdc&#8217;)<br \/>def on_log(args):<br \/> if args[&#8216;log_path&#8217;] == sys.argv[1] #\u8fd9\u91cc\u7684 sys.argv[1]\u5c31\u662f\u4f60\u4f20\u5165 log_path \u7684\u5730\u5740<br \/> print(args[&#8216;data&#8217;])<\/p>\n<p>def scan_log(log_path, app=None, client_id=None):<br \/> &#8230;<br \/> sio.emit(&#8216;log&#8217;, {&#8216;data&#8217;: line.strip(), &#8216;log_path&#8217;: log_path}, namespace=&#8217;\/cdc&#8217;, to=client_id)<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5753327\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Latin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u4e00\u52a0 uuid \u8fc7\u6ee4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5753328\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Alexf4 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @dassh \u611f\u8c22\u4f60\u7684\u601d\u8def~<br \/>&#8211; to \u7684\u53c2\u6570\u6211\u6d4b\u8bd5\u4e86\u4e0b\uff0c \u5728\u4e0d\u8c03\u7528 `start_background_task ` \u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5b8c\u6210\u72ec\u7acb print \u529f\u80fd<br \/>&#8211; \u5728\u8c03\u7528 `start_background_task ` \u624d\u4f1a\u51fa\u73b0\u63d0\u5230\u7684\uff0c\u65e7\u7684\u7a97\u53e3\u4e5f\u4f1a\u83b7\u53d6\u5230\u65b0\u7a97\u53e3\u7684\u6570\u636e\uff08\u65e5\u5fd7\u6570\u636e\uff09<br \/>&#8211; \u6240\u4ee5\u662f\u6bd4\u8f83\u60f3\u597d\u5947\u8fd9\u4e00\u5757\u7684\u5b9e\u73b0\u65b9\u5f0f\u6216\u8005\u539f\u7406                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Flask-SocketIO \u591a\u7ebf&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/439053"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=439053"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/439053\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=439053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=439053"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=439053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}