{"id":102947,"date":"2020-05-14T15:01:30","date_gmt":"2020-05-14T07:01:30","guid":{"rendered":"http:\/\/4563.org\/?p=102947"},"modified":"2020-05-14T15:01:30","modified_gmt":"2020-05-14T07:01:30","slug":"%e5%85%b3%e4%ba%8e%e5%ad%97%e5%85%b8%e7%9a%84%e9%97%ae%e9%a2%98%e3%80%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=102947","title":{"rendered":"\u5173\u4e8e\u5b57\u5178\u7684\u95ee\u9898\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e\u5b57\u5178\u7684\u95ee\u9898\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : s04 <\/span>  <span><i><\/i> 48<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u672c\u4eba\u60f3\u5199\u4e00\u4e2a\u5185\u5b58\u5360\u7528\u7387\u8bb0\u5f55\u7684\u811a\u672c\uff0c\u8fd9\u4e2a\u811a\u672c\u7684\u76ee\u6807\u662f\u5c06\u5185\u5b58\u5360\u7528\u7387\u5927\u4e8e\u767e\u5206\u4e4b 90 \u7684\u65f6\u95f4\u6233\u7528 txt \u6587\u672c\u8bb0\u5f55\u4e0b\u6765\u3002\u4f46\u672c\u4eba\u76ee\u524d\u7528\u5217\u8868\u5199\u51fa\u6765\u7684\u5e76\u4e0d\u80fd\u6267\u884c\u8bb0\u5f55\u529f\u80fd\uff08\u7531 warning_recorder \u51fd\u6570\u5b9e\u73b0\uff09\uff0c\u800c\u4e14\u6211\u89c9\u5f97\u7528\u5b57\u5178\u4ee5\u65f6\u95f4\u4e3a\u952e\uff0c\u5360\u7528\u7387\u4e3a\u503c\u6765\u5efa\u7acb\u5b57\u5178\u6bd4\u7528\u5217\u8868\u66f4\u5408\u9002\uff0c\u6c42\u5927\u4f6c\u6307\u6559\u3002<br \/>\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>from typing import List<\/p>\n<p>import psutil<br \/>import math<br \/>import time<\/p>\n<p>def occ_rate() -&gt;float:<br \/> memory = psutil.virtual_memory()<br \/> occ_rate = float(memory.used) \/ float(memory.total) * 100<br \/>  return float(occ_rate)<br \/>&#8216;_______________________________________________________________________________&#8217;<br \/>def memory_panel():<br \/> while 1:<br \/> memory = psutil.virtual_memory()<br \/> ratio = math.pow(1024, 3) # X Byte = X GB \/ 1024^3<br \/> print(&#8216;\u5df2\u4f7f\u7528\u5185\u5b58\uff1a%.2f GB&#8217; % (memory.used \/ ratio))<br \/> &#8221;&#8217;\u9700\u8981\u4e00\u4e2a\u5229\u7528\u4e0a\u884c\u6570\u636e\u8fdb\u884c\u7edf\u8ba1\u5f97\u51fa\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u4ee5\u53ca\u8d85\u51fa\u8bbe\u5b9a\u9608\u503c\u5e76\u8bb0\u5f55\u6b21\u6570\u3002&#8221;&#8217;<br \/> print(&#8216;\u603b\u5185\u5b58\uff1a%.2f GB&#8217; % (memory.total \/ ratio))<br \/> &#8216;occ_rate = float(memory.used) \/ float(memory.total) * 100&#8217; # \u9700\u8981\u62bd\u51fa\u6765\u5355\u72ec\u505a\u4e00\u4e2a\u51fd\u6570\uff1f<br \/> &#8221;&#8217;occ_rate \u4ee3\u8868\u5185\u5b58\u5360\u7528\u7387&#8221;&#8217;<br \/> m_rate = occ_rate()<br \/> print(&#8220;\u5185\u5b58\u5360\u7528\u7387\uff1a %.2f%%&#8221; % m_rate) # .2f \u662f\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<br \/> print(psutil.swap_memory())<br \/> print(&#8220;\u9000\u51fa\u8bf7\u8f93\u5165 exit(\u8bf7\u533a\u5206\u5927\u5c0f\u5199)\uff1a &#8220;)<br \/> chosen = input() # input()\u65b9\u6cd5\u5c06\u6240\u6709\u8f93\u5165\u503c\u8f6c\u6362\u4e3a string \u7c7b\u578b<br \/> if chosen == &#8220;exit&#8221;:<br \/> print(&#8216;\u518d\u89c1&#8217;)<br \/> exit()<br \/> else:<br \/> continue<br \/>&#8216;_______________________________________________________________________________&#8217;<br \/>def occ_warning_recorder(rate):<br \/> &#8220;&#8221;&#8221;<br \/> :type rate: float<br \/> &#8220;&#8221;&#8221;<br \/> global file<br \/> global now2<br \/> occ_warning: List[float] = []<br \/> while 1:<br \/> rate = occ_rate()<br \/> if rate &lt; 0.95:<br \/> time.sleep(10)<br \/> pass<br \/> elif rate &gt;= 0.95:<br \/> rate: float = rate<br \/> print(&#8216;\/a&#8217;)<br \/> # length_of_w = len(occ_warning)<br \/> for i in range(0,100):<br \/> occ_warning[i] = rate<br \/> i += 1<br \/> now = int(round(time.time() * 1000))<br \/> now2 = time.strftime(&#8216;%Y-%m-%d %H:%M:%S&#8217;, time.localtime(now \/ 1000))<br \/> &#8220;\u4e0a\u9762\u8fd9\u4e24\u884c\u68c0\u6d4b\u901a\u8fc7\uff0c\u6570\u636e\u6b63\u5e38;occ_warning \u4e3a\u8b66\u544a\u503c&#8221;<br \/> file = open(&#8216;G:\/python \u7ec3\u4e60\u518c \/memory_float_log.txt&#8217;,<br \/> &#8216;a&#8217;) # \u5199\u5165\u8b66\u544a\u503c\u5230 log.txt \u5f53\u4e2d,\u5728&#8217;w&#8217;\u5199\u5165\u6a21\u5f0f\u4e0b\uff0c\u5f53\u6211\u4eec\u4e0b\u6b21\u5199\u5165\u53d8\u91cf\u65f6\uff0c\u4f1a\u8986\u76d6 txt\uff0c\u6709\u4e00\u4e2a\u8ffd\u52a0\u6a21\u5f0f&#8217;a&#8217;,\u53ef\u4ee5\u5b9e\u73b0\u591a\u6b21\u5199\u5165<br \/> output_data = [now2, occ_warning[i], &#8216;%&#8217;]<br \/> &#8220;\u2014&#8221;.join(output_data)<br \/> file.write(str(output_data))<br \/> # \u6700\u50bb\u74dc\u7684\u8fde\u63a5\u5b57\u7b26\u4e32\u65b9\u5f0f\uff0c\u4f46\u4e0d\u63a8\u8350\u7528\u8fd9\u79cd\u65b9\u5f0f file.write(str(now02) + &#8216;|&#8217; + str(abv) + &#8216;%&#8217;)<br \/> file.write(&#8216;\/n&#8217;)<br \/> file.close()<br \/>&#8216;_________________________________________________________________________________________________&#8217;<\/p>\n<p>print(memory_panel())<br \/>occupied: float = occ_rate()<br \/>occ_warning_recorder(occupied)      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"706566\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lc1450 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u51fd\u6570 memory_panel \u91cc\u9762\u5f00\u5934 while 1 ? \u51fd\u6570\u7ed3\u5c3e exit or continue \u8fd8\u600e\u4e48\u5f80\u4e0b\u6267\u884c?<br \/>\u5199\u6587\u4ef6\u76f4\u63a5\u5f53\u5b57\u7b26\u4e32\u5199\u5165\u554a, \u5173\u5b57\u5178\u6bdb\u4e8b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"706567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1\u3001\u683c\u5f0f\u529d\u9000\uff0c\u7528 gist \u53d1\uff0c\u8bf7<br \/>2\u3001\u4f60\u5927\u6982\u9700\u8981\u7684\u662f OrderedDict \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"706568\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SjwNo1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u7684\u6211\u8111\u58f3\u75bc                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"706569\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : s04 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @SjwNo1 \u6211\u6ca1\u6709\u597d\u597d\u5199\u6ce8\u91ca\u5427 QAQ                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"706570\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : s04 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &lt;script src=&#8221;https:\/\/gist.github.com\/Leetroch\/90c2c78eff3b4cfca68a6a66a1935441 <button onclick=\"lazyGist(this)\">\u00a0\u663e\u793a Gist \u4ee3\u7801\u00a0<\/button> <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"706571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @s04 \u5148\u8dd1\u4e2a\u9898\uff0c\u5173\u4e8e gist \u7684\u6b63\u786e\u4f7f\u7528\uff0c\u52a0\u4e0a .py \u540e\u7f00\u540d\u53ef\u4ee5\u8c03\u7528\u4ee3\u7801\u9ad8\u4eae\uff0c\u8fd8\u6709 v2 \u80fd\u8ba4\u8bc6\u94fe\u63a5\u672c\u8eab\u4e0d\u8981\u52a0 html\u3002<\/p>\n<p>\u7136\u540e\uff0c\u601d\u7ef4\u6df7\u4e71\uff0c\u4e09\u5206\u4e4b\u4e8c\u7684\u4ee3\u7801\u662f\u6267\u884c\u4e0d\u5230\u7684<br \/>\u8fd8\u6709\u82b1\u5f0f premature optimization \u548c premature design\u3002<br \/>\u6211\u4e0d\u77e5\u9053\u4ece\u54ea\u5f00\u59cb\u8bf4\uff0c\u5148\u8d77\u624b\u5ed6\u96ea\u5cf0\u5427\u3002<br \/>\u5148\u628a\u95ee\u9898\u8bf4\u6e05\u695a\uff0c\u4e0d\u7136\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<p>\u300c\u6211\u73b0\u5728\u78b0\u5230\u4e2a\u96be\u9898\uff0c\u4f60\u6765\u89e3\u51b3\u5427\u3002\u300d                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u5b57\u5178\u7684\u95ee\u9898\u3002 \u8cc7\u6df1\u5927\u4f6c : s&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\/102947"}],"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=102947"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/102947\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102947"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}