{"id":85526,"date":"2020-04-07T09:38:53","date_gmt":"2020-04-07T01:38:53","guid":{"rendered":"http:\/\/4563.org\/?p=85526"},"modified":"2020-04-07T09:38:53","modified_gmt":"2020-04-07T01:38:53","slug":"python-%e5%a4%9a%e8%bf%9b%e7%a8%8b%e9%97%ae%e9%a2%98%e5%92%a8%e8%af%a2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=85526","title":{"rendered":"Python \u591a\u8fdb\u7a0b\u95ee\u9898\u54a8\u8be2"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u591a\u8fdb\u7a0b\u95ee\u9898\u54a8\u8be2               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : frmongo <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5404\u4f4d\u5e05\u54e5\uff0c\u6211\u5199\u4e86\u4e2a\u811a\u672c\uff0c\u4f5c\u7528\u662f\uff0c\u6301\u7eed\u5730\u628a\u4e32\u53e3\u7684\u6253\u5370\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u672c\u91cc\uff0c\u7136\u540e\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65f6\u95f4\u540e\u505c\u6b62\u3002 \u4ee3\u7801\u5982\u4e0b\uff0c\u4f46\u662f\u505c\u6b62\u540e\uff0c\u4f3c\u4e4e\u56e0\u4e3a\u6ca1\u6709\u4fdd\u5b58\uff0c\u5bfc\u81f4\u6587\u672c\u662f\u7a7a\u7684\uff0c\u600e\u4e48\u5199\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u76ee\u7684\u5462\u3002<\/p>\n<pre><code>import serial from multiprocessing import Process as process from time import sleep import sys  def get_print(comport):     ser = serial.Serial(comport,baudrate=115200,bytesize=8,parity='N',stopbits=1,timeout=1)     with open('m3.txt','w',encoding=\"gb18030\") as f:         while 1:             if ser.inWaiting():                 x=ser.read(150)                 lll = x.decode('gb18030','replace')                 f.write(lll)   if __name__ == '__main__':     p1 = process(target=get_print, args=('com7',))     p1.daemon=True     p1.start()     sleep(10)     sys.exit('ok') <\/code><\/pre>\n<\/p><\/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=\"1426917\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hstar <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e24\u79cd\u601d\u8def\uff0c\u5176\u5b9e\u6ca1\u533a\u522b\u90fd\u662f\u5728 get_print \u51fd\u6570\u7684 while 1 \u8fd9\u4e2a\u5730\u65b9\u505a\u6587\u7ae0<br \/>1. \u628a 10 \u8fd9\u4e2a\u53d8\u91cf\u4f20\u8fdb get_print \u51fd\u6570\uff0c\u5728 while 1 \u8fd9\u4e2a\u5730\u65b9\u4e00\u76f4\u6bd4\u5bf9\u662f\u5426\u8d85\u8fc7 10 \u79d2\u4e86<br \/>2. \u770b\u4e00\u4e0b\u591a\u7ebf\u7a0b \/\u8fdb\u7a0b\u7684 Event \u5bf9\u8c61\uff0c\u521d\u59cb\u5316\u4e00\u4e2a\u9501\u4f20\u8fdb get_print \u51fd\u6570\uff0cwhile 1 \u8fd9\u4e2a\u5730\u65b9\u89c2\u5bdf\u9501\u662f\u5426\u8fd8\u5728\uff0c\u5341\u79d2\u540e\u628a\u8fd9\u4e2a\u9501 clear \u6389\uff0c\u8ba9 get_print \u505c\u4f4f<br \/>\u65e0\u8bba\u600e\u4e48\u6837 sys.exit(&#8216;ok&#8217;)\u90fd\u8981\u6362\u6210 p1.join()                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1426918\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : renmu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u63d0\u9192\u4e00\u4e0b\u591a\u8fdb\u7a0b\u6587\u4ef6\u5199\u5165\u4f1a\u5bfc\u81f4\u6df7\u4e71\uff0c\u8fd9\u4ef6\u4e8b\u5e94\u8be5\u7528\u5355\u8fdb\u7a0b\u5c31\u80fd\u7528\uff0c\u4f60\u7528 sleep \u76f4\u63a5\u628a\u4e3b\u8fdb\u573a\u5835\u6b7b\u4e86\uff0c\u56db\u820d\u4e94\u5165\u8fd8\u662f\u53ea\u6709\u4e00\u4e2a\u8fdb\u7a0b\u5728\u5de5\u4f5c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1426919\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BingoXuan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 Thread \u7c7b\u542f\u52a8\u7ebf\u7a0b\uff0c\u52a0\u4e00\u4e2a\u5b9e\u4f8b\u53d8\u91cf\u63a7\u5236\u7ebf\u7a0b\u8fd0\u884c\uff0c\u65f6\u95f4\u5230\u4e86\u5c31\u4fee\u6539\u53d8\u91cf\u503c\u505c\u6b62\u7ebf\u7a0b\u5e76\u4fdd\u5b58\u6570\u636e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1426920\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Kilerd <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Python \u53ea\u80fd\u5355\u7ebf\u7a0b\uff08                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1426921\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/www.runoob.com\/python\/file-flush.html<\/p>\n<p>\u7531\u4e8e\u4f60\u53ea\u6709\u4e00\u4e2a thread \u5728\u5199\uff0c\u53ea\u9700\u8981 flush \u64cd\u4f5c\u5c31\u884c\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1426922\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : crella <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             while \u5224\u65ad\u5168\u5c40\u53d8\u91cf\uff0c\u5982\u679c false\uff0c\u5219\u5173\u95ed\u6587\u4ef6 io\uff0c\u540c\u65f6 break                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u591a\u8fdb\u7a0b\u95ee\u9898\u54a8\u8be2 \u8cc7\u6df1&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\/85526"}],"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=85526"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85526\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85526"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}