{"id":89,"date":"2017-02-01T08:20:10","date_gmt":"2017-02-01T00:20:10","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=89"},"modified":"2017-02-01T08:20:10","modified_gmt":"2017-02-01T00:20:10","slug":"python-article0004","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/01\/python-article0004\/","title":{"rendered":"Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b"},"content":{"rendered":"<h2>Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;<\/h2>\n<h2>\u5728\u5355\u7ebf\u7a0b\u4e2d<\/h2>\n<pre><code># -*- coding=utf8 -*-\nimport threading\nfrom time import ctime, sleep\n#\u5355\u7ebf\u7a0b\u5b9e\u4f8b\ndef frist(frist_name, length):\n    for i in range(4):\n        print('I was listening to %s %s'% (frist_name, ctime()))\n        sleep(length)\ndef second(second_name, length):\n    for i in range(2):\n        print('I was at the movie %s %s'% (second_name, ctime()))\n        sleep(length)\nif __name__ == '__main__':\n    frist('\u7b2c\u4e00\u7ebf\u7a0b', 2)\n    second('\u7b2c\u4e8c\u7ebf\u7a0b', 4)\n    print('all over %s'% ctime())\n    &lt;\/pre&gt;\n<\/code><\/pre>\n<h4>\u7ed3\u679c<\/h4>\n<pre><code>I was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:13:07 2017\nI was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:13:09 2017\nI was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:13:11 2017\nI was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:13:13 2017\nI was at the movie \u7b2c\u4e8c\u7ebf\u7a0b Tue Jan 17 23:13:15 2017\nI was at the movie \u7b2c\u4e8c\u7ebf\u7a0b Tue Jan 17 23:13:19 2017\nall over Tue Jan 17 23:13:23 2017\n<\/code><\/pre>\n<h2>\u591a\u7ebf\u7a0b\u4e2d<\/h2>\n<ul>\n<li>\u5728\u591a\u7ebf\u7a0b\u4e2d\uff0c\u7ebf\u7a0b\u7684\u8c03\u7528\u65f6\u968f\u673a\u7684\uff0c\u6240\u4ee5\u6bcf\u6b21\u7684\u7ed3\u679c\u5e76\u4e0d\u4e00\u6837<\/li>\n<\/ul>\n<pre><code># -*- coding=utf8 -*-\nimport threading\nfrom time import ctime, sleep\n#\u591a\u7ebf\u7a0b\ndef frist(frist_name, length):\n    for i in range(4):\n        print('I was listening to %s %s'% (frist_name, ctime()))\n        sleep(length)\ndef second(second_name, length):\n    for i in range(2):\n        print('I was at the second %s %s'% (second_name, ctime()))\n        sleep(length)\nth1 = threading.Thread(target=frist, args=('\u7b2c\u4e00\u7ebf\u7a0b', 1))\nth2 = threading.Thread(target=second, args=('\u7b2c\u4e8c\u7ebf\u7a0b', 2))\nthreads = [th1,th2]\n\nif __name__ == '__main__':\n    for t in threads:\n        t.setDaemon(True)\n        t.start()\n    t.join()\nprint('all over %s' % ctime())&lt;\/pre&gt;\n<\/code><\/pre>\n<p>####\u7ed3\u679c<\/p>\n<pre><code>I was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:25:22 2017\nI was at the second \u7b2c\u4e8c\u7ebf\u7a0b Tue Jan 17 23:25:22 2017\nI was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:25:23 2017\nI was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:25:24 2017\nI was at the second \u7b2c\u4e8c\u7ebf\u7a0b Tue Jan 17 23:25:24 2017\nI was listening to \u7b2c\u4e00\u7ebf\u7a0b Tue Jan 17 23:25:25 2017\nall over Tue Jan 17 23:25:26 2017\n<\/code><\/pre>\n<ul>\n<li>threading\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u6a21\u5757\uff0c\u6709\u4e9b\u670b\u53cb\u67e5\u5230\u4f1a\u6709thread\u8fd9\u4e2a\u6a21\u5757\uff0c\u4f46\u662f\u5728python3\u91cc\u9762\u53ea\u5269\u4e0bthreading\u8fd9\u4e2a\u6a21\u5757\u4e86\uff0c\u56e0\u4e3athreading\u6a21\u5757\u7528\u8d77\u6765\u66f4\u7b80\u5355\u4e5f\u66f4\u5b89\u5168\u4e00\u4e9b<\/li>\n<\/ul>\n<ol>\n<li>time\u6a21\u5757\u4e2d\u6709ctime()\uff0csleep()\u7b49\u51fd\u6570\uff0cctime()\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\uff0c\u7528\u4e00\u4e2astr\u8868\u793a\uff0csleep(n)\u8868\u793a\u6302\u8d77n\u79d2<\/p>\n<\/li>\n<li>\n<p>import threading  \u5bfc\u5165threading\u6a21\u5757<\/p>\n<\/li>\n<li>\n<p>th1 = threading.Thread(target=music, args=('\u7b2c\u4e00\u7ebf\u7a0b',1))  \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0bth1\uff0cthreading.Thread()\u662f\u4e00\u4e2a\u7c7b\uff0c\u7c7b\u7684\u6784\u9020\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a  class threading.Thread(group=None,target=None, name=None, args=(), kwargs={}, *, daemon=None)  \u8fd9\u91cc\u7528\u5230\u4e86target\uff0c\u8868\u793a\u8981\u8c03\u7528\u7684\u51fd\u6570\u540d\uff0cargs\u8868\u793a\u8c03\u7528\u51fd\u6570\u7684\u53c2\u6570<\/p>\n<\/li>\n<\/ol>\n<pre><code>threads = [th1, th2]\n\u5c06\u4e24\u4e2a\u7ebf\u7a0b\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\nfor t in threads:\n    t.setDaemon(True)\n    t.start()\nt.join()\n\u6700\u540e\u4f7f\u7528\u4e00\u4e2afor\u5faa\u73af\uff0c\u4f9d\u6b21\u5c06\u5217\u8868\u4e2d\u7684\u7ebf\u7a0b\u5f00\u542f\nt.setDaemon(True)\n<\/code><\/pre>\n<blockquote>\n<p>\u5c06\u7ebf\u7a0b\u58f0\u660e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5fc5\u987b\u5728start() \u65b9\u6cd5\u8c03\u7528\u4e4b\u524d\u8bbe\u7f6e\uff0c\u5982\u679c\u4e0d\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u7a0b\u5e8f\u4f1a\u88ab\u65e0\u9650\u6302\u8d77\u3002\u5b50\u7ebf\u7a0b\u542f\u52a8\u540e\uff0c\u7236\u7ebf\u7a0b\u4e5f\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\uff0c\u5f53\u7236\u7ebf\u7a0b\u6267\u884c\u5b8c\u6700\u540e\u4e00\u6761\u8bed\u53e5print \"all over %s\" %ctime()\u540e\uff0c\u6ca1\u6709\u7b49\u5f85\u5b50\u7ebf\u7a0b\uff0c\u76f4\u63a5\u5c31\u9000\u51fa\u4e86\uff0c\u540c\u65f6\u5b50\u7ebf\u7a0b\u4e5f\u4e00\u540c\u7ed3\u675f\u3002<br \/>\n  t.start()<br \/>\n  \u5f00\u59cb\u7ebf\u7a0b\u6d3b\u52a8\u3002<br \/>\n  t.join()\n<\/p><\/blockquote>\n<ul>\n<li>join()\u65b9\u6cd5\uff0c\u7528\u4e8e\u7b49\u5f85\u7ebf\u7a0b\u7ec8\u6b62\u3002join\uff08\uff09\u7684\u4f5c\u7528\u662f\uff0c\u5728\u5b50\u7ebf\u7a0b\u5b8c\u6210\u8fd0\u884c\u4e4b\u524d\uff0c\u8fd9\u4e2a\u5b50\u7ebf\u7a0b\u7684\u7236\u7ebf\u7a0b\u5c06\u4e00\u76f4\u7b49\u5f85\u3002<\/li>\n<\/ul>\n<blockquote><p>\n  \u6ce8\u610f:  join()\u65b9\u6cd5\u7684\u4f4d\u7f6e\u662f\u5728for\u5faa\u73af\u5916\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u5fc5\u987b\u7b49\u5f85for\u5faa\u73af\u91cc\u7684\u4e24\u4e2a\u8fdb\u7a0b\u90fd\u7ed3\u675f\u540e\uff0c\u624d\u53bb\u6267\u884c\u4e3b\u8fdb\u7a0b\u3002\u5982\u679c\u6ca1\u6709join()\u51fd\u6570\uff0c\u90a3\u4e48\u7236\u7ebf\u7a0b\u6267\u884c\u5b8c\u4e4b\u540e\u5c31\u4f1a\u7acb\u5373\u7ed3\u675f\uff0c\u4e0d\u4f1a\u7b49\u5f85\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b;Python\u7ebf\u7a0b\u6982\u5ff5\u4e0e\u5b9e\u4f8b\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/01\/python-article0004\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":97,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[15],"tags":[58,84],"class_list":["post-89","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-python","tag-python","tag-thread","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/97"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}