{"id":154,"date":"2015-12-24T02:10:46","date_gmt":"2015-12-24T02:10:46","guid":{"rendered":""},"modified":"2015-12-24T02:10:46","modified_gmt":"2015-12-24T02:10:46","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=154","title":{"rendered":"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65"},"content":{"rendered":"<p>\n\t<span style=\"font-size:14px;\">Python\u4e2d\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u5e95\u5c42\uff08low-level\uff09\u7684thread\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u9ad8\u5c42(high-level)\u7684threading. \u4e24\u8005\u90fd\u53ef\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0cthread\u6a21\u5757\u4f7f\u7528\u7684\u662f\u4f4e\u7ea7\u7684\u539f\u8bed\uff0cthreading\u6a21\u5757\u57fa\u4e8ethread\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9ad8\u5c42\u7684API\uff0c\u66f4\u7b80\u5355\u6613\u7528\uff0c\u6240\u4ee5\u63a8\u8350\u4f7f\u7528threading\u6a21\u5757\u3002<\/span><br \/>\n<strong><span style=\"font-size:14px;\">\u6ce8\u610f<\/span><\/strong><span style=\"font-size:14px;\">\uff1a\u5728Python\u4e2d\uff0c\u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u662f\u4e0d\u5408\u7406\u7684\u3002\u56e0\u4e3aPython\u7684\u4e3b\u6d41\u7248\u672cCPython\u4e2d\uff0c\u5b58\u5728\u4e00\u4e2aGlobal Interpreter Lock\uff08GIL\uff09\u7684\u5168\u5c40\u89e3\u91ca\u9501,\u8fd9\u4e2a\u9501\u7684\u4f5c\u7528\u662f\u4fdd\u8bc1\u5728\u4e00\u4e2a\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u6267\u884cpython\u5b57\u8282\u7801.\u4e00\u4e2a\u7ebf\u7a0b\u5fc5\u987b\u5148\u6301\u6709\u8fd9\u4e2a\u9501,\u624d\u80fd\u6267\u884c\u5b57\u8282\u7801\u3002\u8fd9\u5c31\u8868\u793a\uff0c\u540c\u4e00\u4e2a\u65f6\u523b\u4e0d\u4f1a\u6709\u591a\u4e2a\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u3002<\/span>\n<\/p>\n<p><strong><span style=\"font-size:14px;\">thread\u6a21\u5757<\/span><\/strong> <\/p>\n<p>\n\t<span style=\"font-size:14px;\">thread\u6a21\u5757\u4f7f\u7528\u4f4e\u7ea7\u7684\u539f\u8bed\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\uff0c\u901a\u8fc7\u63d0\u4f9b \u9501\uff08lock object\uff09\u673a\u5236\uff0c\u540c\u4e00\u4e2a\u65f6\u523b\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5360\u7528\u9501\uff0c\u5229\u7528\u9501\u7684\u83b7\u53d6\uff08lock.acquire\uff09\u548c\u9501\u7684\u91ca\u653e\uff08lock.release\uff09\u5b9e\u73b0\u5bf9\u8c61\u7684\u4e92\u65a5\u8bbf\u95ee\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">thread\u6a21\u5757\u7684\u4e3b\u8981\u51fd\u6570\u6709\uff1a<\/span><br \/>\n<span style=\"font-size:14px;\">thread.start_new_thread(function, args[, kwargs]): \u542f\u52a8\u4e00\u4e2a\u65b0\u7ebf\u7a0b\uff0c\u5e76\u8fd4\u56de\u7ebf\u7a0b\u7684\u6807\u8bc6\u3002function\u662f\u7ebf\u7a0b\u7684\u56de\u8c03\u51fd\u6570\uff1bargs\u662f\u4f20\u9012\u7ed9\u56de\u8c03\u51fd\u6570\u7684\u4e00\u4e2atuple\uff1bkwargs\u662f\u53ef\u9009\u53c2\u6570\uff0c\u4ee3\u8868\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4e00\u4e2a\u5b57\u5178\uff08a dictionary of keyword arguments\uff09\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">thread.exit() : \u629b\u51fa\u4e00\u4e2aSystemExit\u5f02\u5e38\uff0c\u5982\u679c\u8be5\u5f02\u5e38\u6ca1\u88ab\u6355\u83b7\uff0c\u7ebf\u7a0b\u5c31\u81ea\u52a8\u9759\u9ed8\u9000\u51fa\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">thread.exit_thread(): \u9000\u51fa\u7ebf\u7a0b<\/span><br \/>\n<span style=\"font-size:14px;\">thread.get_ident(): \u8fd4\u56de\u7ebf\u7a0b\u7684\u6807\u8bc6\uff08\u975e0\u6574\u6570\uff09<\/span><br \/>\n<span style=\"font-size:14px;\">thread.allocate_lock(): \u8fd4\u56de\u4e00\u4e2a\u65b0\u5efa\u7684\u9501\u3002\u521d\u59cb\u5316\u72b6\u6001\u4e3aunlock\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">lock.acquire([waitflag]): \u8bf7\u6c42\u83b7\u53d6\u9501\uff0c\u6210\u529f\u8fd4\u56deTrue\uff0c\u5426\u5219\u8fd4\u56deFalse. waitflag\u7701\u7565\u6216\u975e0\u503c\uff0c\u5219\u65e0\u6761\u4ef6\u7b49\u5f85\uff0c\u76f4\u5230\u5176\u5b83\u7ebf\u7a0b\u91ca\u653e\u4e86\u9501\uff0c\u672c\u7ebf\u7a0b\u80fd\u591f\u83b7\u53d6\u9501\uff0cwaitflag=0\u8868\u793a\u65e0\u8bba\u662f\u5426\u83b7\u53d6\u6210\u529f\uff0c\u51fd\u6570\u9a6c\u4e0a\u8fd4\u56de\uff0c\u4e0d\u7b49\u5f85\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">lock.release(): \u91ca\u653e\u9501\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">lock.locked(): \u67e5\u770b\u9501\u5f53\u524d\u662f\u5426\u5904\u4e8e\u9501\u5b9a\u72b6\u6001\uff0c\u662f\u5219\u8fd4\u56deTrue,\u5426\u5219\u8fd4\u56deFalse.<\/span>\n<\/p>\n<pre class=\"brush: python;auto-links: false;\">#coding=utf-8\r\n#author: JarvisChu\r\n#date: 2014-3-3\r\n#blog:zhujiangtao.com\r\n \r\nimport thread,time\r\n \r\n#\u603b\u7968\u6570\r\ntotal_ticket = 10\r\n \r\n#\u552e\u7968\u7a0b\u5e8f\r\ndef sell(name,ticket_lock):\r\n    '''\u542f\u52a8\u4e00\u4e2a\u552e\u7968\u7a97\u53e3\uff0c\u5f00\u59cb\u552e\u7968\uff0c\u6bcf\u6b21\u552e\u51fa1\u5f20\u3002name \u552e\u7968\u7a97\u53e3\u7684\u540d\u5b57\uff1bticket_lock \u540c\u6b65\u9501'''\r\n    #print name,'\u542f\u52a8,id=',thread.get_ident()\r\n    while True:\r\n        if ticket_lock.acquire():\r\n            #print id,ticket_lock.locked()\r\n            try:\r\n                global total_ticket #\u58f0\u660e\u8fd9\u662f\u5168\u5c40\u53d8\u91cf\u7684\r\n                if  total_ticket &lt;= 0:\r\n                    #print '\u7968\u5df2\u552e\u5b8c'\r\n                    break\r\n                else:\r\n                    print name,'\u552e\u51fa\u8f66\u7968:',total_ticket\r\n                    total_ticket = total_ticket - 1\r\n            finally:\r\n                ticket_lock.release()\r\n    thread.exit_thread()\r\n<\/pre>\n<p><\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t#\u65b0\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\uff0c\u7528\u4e8e\u540c\u6b65\u552e\u7968<br \/>\nlock = thread.allocate_lock()<br \/>\n#print lock.locked() #\u67e5\u770b\u9501\u7684\u72b6\u6001<br \/>\n#\u542f\u52a8\u4e09\u4e2a\u7a97\u53e3\uff0c\u540c\u65f6\u552e\u7968<br \/>\nid1 = thread.start_new_thread(sell,(&#8216;\u552e\u7968\u7a97\u53e31&#8217;, lock))<br \/>\nid2 = thread.start_new_thread(sell,(&#8216;\u552e\u7968\u7a97\u53e32&#8217;, lock))<br \/>\nid3 = thread.start_new_thread(sell,(&#8216;\u552e\u7968\u7a97\u53e33&#8217;, lock))<br \/>\n#print id1,id2,id3<br \/>\n#\u4e3b\u7ebf\u7a0b\u7b49\u5f852\u79d2<br \/>\ntime.sleep(2)\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\">&nbsp;<\/span>\n<\/p>\n<p><strong><span style=\"font-size:14px;\">threading\u6a21\u5757<\/span><\/strong> <\/p>\n<p>\n\t<span style=\"font-size:14px;\">\u57fa\u4e8ethread\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u3002\u5305\u542b\u7684\u5185\u5bb9\u6709\uff1a\u4e00\u4e2a\u7ebf\u7a0b\u7c7bThread\uff0c\u51e0\u4e2a\u7528\u4e8e\u540c\u6b65\u7684\u5bf9\u8c61Lock\u3001RLock\u3001Condition\u3001Event\u3001Semaphore\uff0c\u51e0\u4e2a\u8f85\u52a9\u7684\u51fd\u6570\u548c\u5bf9\u8c61\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">threading\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2athreading.Thread\u7c7b\uff0c\u8868\u793a\u4e00\u4e2a\u7ebf\u7a0b\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u83b7\u5f97\u81ea\u5df1\u7684\u7ebf\u7a0b\uff1a<\/span><br \/>\n<span style=\"font-size:14px;\">1. \u7ed9Thread\u7c7b\u7684\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570target\u4f20\u9012\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u4f1a\u88ab\u7ebf\u7a0b\u7684run()\u51fd\u6570\u8c03\u7528<\/span>\n<\/p>\n<p><pre class=\"brush: python;auto-links: false;\">#coding=utf-8\r\n \r\nimport threading,time\r\n \r\ndef foo(no,workname):\r\n    print '%d \u53f7\u552e\u7968\u5458%s \u4e3a\u60a8\u670d\u52a1..' % (no,workname)\r\n \r\nthread1 = threading.Thread(target=foo,args=(1,'Seller1'))\r\nthread2 = threading.Thread(target=foo,args=(2,'Seller2'))\r\nthread1.start()\r\nthread2.start()\r\n \r\ntime.sleep(3)<\/pre>\n<p>\n<span style=\"font-size:14px;\"><\/span>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\">2. \u7ee7\u627fThread\u7c7b\uff0c\u8986\u5199\uff08overwrite\uff09run()\u65b9\u6cd5<\/span>\n<\/p>\n<pre class=\"prettyprint lang-py linenums\">#coding=utf-8\r\nimport threading,time\r\n \r\nclass Sellman(threading.Thread):#1.\u7ee7\u627fThread\r\n    def __init__(self,no,workname):\r\n        threading.Thread.__init__(self)#2.\u8c03\u7528__init__\r\n        self.no = no\r\n        self.workname = workname\r\n \r\n    def run(self):#3.\u8986\u5199run\r\n        print '%d \u53f7\u552e\u7968\u5458%s \u4e3a\u60a8\u670d\u52a1..' % (self.no,self.workname)\r\n \r\nseller1 = Sellman(1,'Seller1')\r\nseller2 = Sellman(1,'Seller2')\r\nseller1.start()\r\nseller2.start()\r\n \r\ntime.sleep(3)<\/pre>\n<p><span style=\"font-size:14px;\">threading.Thread\u7c7b<\/span><br \/>\n<span style=\"font-size:14px;\">\u6784\u9020\u51fd\u6570\uff1athreading.Thread(group=None, target=None, name=None, args=(), kwargs={})<\/span><br \/>\n<span style=\"font-size:14px;\">group\u4e3a\u4fdd\u7559\u53c2\u6570\uff0c\u6682\u65f6\u65e0\u7528\uff1btarget\u4e3a\u53ef\u8c03\u7528\u7684\u5bf9\u8c61\uff1bname\u4e3a\u7ebf\u7a0b\u7684\u540d\u5b57\uff0c\u6ce8\u610f\u591a\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6709\u76f8\u540c\u7684\u540d\u5b57\uff1bargs\u662f\u4e00\u4e2a\u53c2\u6570\u7684tuple\uff1bkwargs\u4e3a\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5b57\u5178\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">name,getName(),setName(): \u83b7\u53d6\u548c\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u540d\u5b57<\/span><br \/>\n<span style=\"font-size:14px;\">ident: \u7ebf\u7a0b\u7684\u552f\u4e00\u6807\u8bc6\u3002\u7ebf\u7a0b\u542f\u52a8\u540e\u5c31\u4f1a\u6709\uff0c\u7ebf\u7a0b\u9000\u51fa\u540e\u88ab\u56de\u6536<\/span><br \/>\n<span style=\"font-size:14px;\">start(): \u542f\u52a8\u7ebf\u7a0b<\/span><br \/>\n<span style=\"font-size:14px;\">join(): \u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7ed3\u675f\u540e\u518d\u8fd0\u884c\u3002 join\u628a\u6307\u5b9a\u7684\u7ebf\u7a0b\u52a0\u5165\u5230\u5f53\u524d\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u4ea4\u66ff\u6267\u884c\u7684\u7ebf\u7a0b\u5408\u5e76\u4e3a\u987a\u5e8f\u6267\u884c\u7684\u7ebf\u7a0b\u3002\u6bd4\u5982\u5728\u7ebf\u7a0bB\u4e2d\u8c03\u7528\u4e86\u7ebf\u7a0bA\u7684join()\u65b9\u6cd5\uff0c\u76f4\u5230\u7ebf\u7a0bA\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u624d\u4f1a\u7ee7\u7eed\u6267\u884c\u7ebf\u7a0bB\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">is_alive(): \u8fd4\u56de\u7ebf\u7a0b\u662f\u5426\u8fd8\u5b58\u6d3b\u7740\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">daemon, isDaemon,setDaemon: \u5224\u65ad\u7ebf\u7a0b\u662f\u5426\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\uff08\u6ca1\u6709\u7528\u6237\u7ebf\u7a0b\u65f6\u81ea\u52a8\u9000\u51fa\u7684\u7ebf\u7a0b\uff0c\u5982\u5783\u573e\u56de\u6536\u7ebf\u7a0b\uff09\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">threading.Lock()<\/span><br \/>\n<span style=\"font-size:14px;\">\u751f\u6210\u4e00\u4e2aLock\u9501\u5bf9\u8c61\uff0c\u540cthread\u6a21\u5757\u4e2d\u7684thread.allocate_lock\uff0c\u9501\u7684\u7528\u6cd5\u4e5f\u76f8\u540c\uff0c\u4f7f\u7528acquire()\u83b7\u53d6\uff0crelease()\u91ca\u653e\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">threading.RLock()<\/span><br \/>\n<span style=\"font-size:14px;\">\u751f\u6210\u4e00\u4e2a\u53ef\u91cd\u5165(reentrant)\u7684RLock\u9501\u5bf9\u8c61\u3002RLock\u53ef\u4ee5\u88ab\u540c\u4e00\u4e2a\u7ebf\u7a0b\u8fde\u7eed\u591a\u6b21acquire\uff0c\u7ebf\u7a0bacquire\u7684\u6b21\u6570\u8981\u548crelease\u6b21\u6570\u76f8\u540c\u3002\uff08Lock\u9501\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u5982\u679cacquire\u540e\u6ca1\u6709\u91ca\u653e\uff0c\u518dacquire\u5219\u4f1a\u4f7f\u8be5\u7ebf\u7a0b\u6302\u8d77\uff09&nbsp;&nbsp; RLock\u5185\u90e8\u4f7f\u7528 recursion level\uff0c\u6bcf\u6b21acquire\uff0crecursion level\u4f1a++\uff0creleased\u5219\u4f1a\u2013\u3002\u4e00\u65e6recursion level \u7b49\u4e8e0\uff0c\u5219\u8868\u793a\u8be5\u7ebf\u7a0b\u91ca\u653e\u4e86\u8be5RLock\u9501\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">threading. Condition()<\/span><br \/>\n<span style=\"font-size:14px;\">\u751f\u6210\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf(Condition Variable) \u5bf9\u8c61\u3002Condition Variable\u5bf9\u8c61\u603b\u662f\u548c\u9501\u4e00\u8d77\u5de5\u4f5c\uff0c\u8fd9\u4e2a\u9501\u53ef\u4ee5\u662f\u4f60\u4f20\u9012\u7ed9\u5b83\u7684\u6216\u8005\u5b83\u9ed8\u8ba4\u521b\u5efa\u7684\u3002Condition Variable\u5bf9\u8c61 \u901a\u8fc7acqure()\u548crelease()\u4e24\u4e2a\u65b9\u6cd5\u64cd\u4f5c\u5b83\u5185\u90e8\u7684\u9501\u3002\u5f53Condition Variable\u5bf9\u8c61acquire()\u5f97\u5230\u4e00\u4e2a\u9501\u7684\u5360\u7528\u6743\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528wait()\u3001notify()\u548cnotifyall()\u65b9\u6cd5\u3002\uff08\u5982\u679c\u6ca1\u6709\u5360\u7528\u9501\u5c31\u8c03\u7528\u8fd9\u4e2a\u4e09\u4e2a\u65b9\u6cd5\uff0c\u4f1a\u629b\u51faRuntimeError\u5f02\u5e38\uff09\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">wait(): \u91ca\u653e\u9501\uff0c\u7136\u540e\u6302\u8d77\uff08block\uff09\uff0c\u76f4\u5230\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5bf9\u8fd9\u4e2a\u76f8\u540c\u7684Condition Variable\u5bf9\u8c61\u8c03\u7528\u4e86notify()\u6216notifyall()\u65f6\uff0c\u624d\u88ab\u5524\u9192\u3002\u5524\u9192\u540e\uff0c\u5b83\u91cd\u65b0\u83b7\u5f97\u9501\u7684\u5360\u6709\u6743\u5e76\u8fd4\u56de\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">notify() \u548c notifyall(): \u5524\u9192\u4e00\u4e2a\u6216\u6240\u6709\u7684\u56e0\u7b49\u5f85\u8fd9\u4e2aCondition Variable\u5bf9\u8c61\u800c\u6302\u8d77\u7684\u7ebf\u7a0b\u3002\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4e0d\u4f1arelease\u9501\uff0c\u8fd9\u610f\u5473\u7740\uff0c\u5524\u9192\u7684\u7ebf\u7a0b\u4e0d\u4f1a\u7acb\u5373\u4ece\u5b83(\u4eec)\u7684wait()\u65b9\u6cd5\u8fd4\u56de\uff0c\u76f4\u5230\u8c03\u7528notify()\u6216notifyall()\u7684\u7ebf\u7a0b\u653e\u5f03\u4e86\u9501\u7684\u5360\u6709\u6743\u540e\uff0c\u624d\u80fd\u83b7\u53d6\u5360\u6709\u6743\u5e76\u8fd4\u56de\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">Condition Variable \u7684\u5178\u578b\u5e94\u7528\u573a\u666f\u662f\u4f7f\u7528\u9501\u5bf9\u5171\u4eab\u72b6\u6001\uff08shared state\uff09\u7684\u8bbf\u95ee\u8fdb\u884c\u540c\u6b65\u3002\u4e00\u4e2a\u7ebf\u7a0b\u91cd\u590d\u8c03\u7528wait() \u76f4\u5230 \u5171\u4eab\u72b6\u6001\u53d8\u5316\u4e3a\u5b83\u9700\u8981\u7684\u72b6\u6001\u3002\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u5b8c\u72b6\u6001\u540e\uff0c\u8c03\u7528notify()\u901a\u77e5\u5176\u5b83\u7b49\u5f85\u8be5\u72b6\u6001\u7684\u7ebf\u7a0b\u3002\u5178\u578b\u7684\u5e94\u7528\u5982\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\u3002<\/span> <\/p>\n<pre class=\"prettyprint lang-py linenums\"># \u6d88\u8d39\u4e00\u4e2a\u7269\u54c1 - \u6d88\u8d39\u8005\u7ebf\u7a0b\r\ncv.acquire()  #\u83b7\u53d6\u9501\r\nwhile not an_item_is_available():\r\n    cv.wait() #\u5982\u679c\u6ca1\u6709\u7269\u54c1\u53ef\u7528\uff0c\u5219\u8c03\u7528wait()\u91ca\u653e\u9501\uff0c\u7136\u540e\u6302\u8d77\u7b49\u5f85\r\nget_an_available_item() #\u6d88\u8d39\u4e00\u4e2a\u7269\u54c1\r\ncv.release() #\u91ca\u653e\u9501\r\n \r\n# \u751f\u6210\u4e00\u4e2a\u7269\u54c1 - \u751f\u4ea7\u8005\u7ebf\u7a0b\r\ncv.acquire() #\u83b7\u53d6\u9501\r\nmake_an_item_available() #\u751f\u4ea7\u4e00\u4e2a\u7269\u54c1\r\ncv.notify() #\u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u7ebf\u7a0b\uff0c\u8fd9\u4e2a\u7b49\u5f85\u7ebf\u7a0b\u9192\u4e86\uff0c\u4f46\u6b64\u65f6\u8fd8\u4e0d\u80fd\u83b7\u53d6\u9501\uff0c\u56e0\u4e3a\u751f\u4ea7\u8005\u7ebf\u7a0b\u8fd8\u6ca1\u6709\u91ca\u653e\u9501\r\ncv.release() #\u91ca\u653e\u9501\uff0c\u6b64\u65f6\u6d88\u8d39\u8005\u7ebf\u7a0bwait()\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u9501\uff0c\u5e76\u8fd4\u56de<\/pre>\n<p>\n\t<span style=\"font-size:14px;\">threading.Event()<\/span><br \/>\n<span style=\"font-size:14px;\">\u751f\u6210\u4e00\u4e2a Event \u5bf9\u8c61\u3002Event \u662f\u6700\u7b80\u5355\u7684\u7ebf\u7a0b\u901a\u4fe1\u673a\u5236\u4e4b\u4e00\uff0c\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u5185\u90e8\u7684flag\u7684\u72b6\u6001\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65\u3002\u901a\u8fc7set()\u65b9\u6cd5\u5c06Event\u5bf9\u8c61\u7684flag\u8bbe\u7f6e\u4e3aTrue\uff0c\u901a\u8fc7clear()\u65b9\u6cd5\u5c06Event\u5bf9\u8c61reset\u4e3aFasle. wait()\u65b9\u6cd5\u5c06\u7ebf\u7a0b\u6302\u8d77\uff0c\u76f4\u5230Event\u4e3aTrue.<\/span><br \/>\n<span style=\"font-size:14px;\">threading.Semaphore([value])<\/span><br \/>\n<span style=\"font-size:14px;\">\u751f\u6210\u4e00\u4e2aSemaphore\u5bf9\u8c61\uff08\u4fe1\u53f7\u91cf\u5bf9\u8c61\uff09\u3002Semaphore\u5bf9\u8c61\u7ef4\u62a4\u4e00\u4e2a\u5185\u90e8\u7684\u8ba1\u6570\u5668\u3002\u8ba1\u6570\u5668\u7684\u521d\u59cb\u503c\u4e3avalue,\u5982\u679c\u6ca1\u7ed9\u5b9avalue\u503c\uff0c\u5219\u9ed8\u8ba4\u4e3a1. \u8c03\u7528release()\u65b9\u6cd5\uff0c\u8ba1\u65f6\u5668\u503c+1\uff0c\u8c03\u7528acquire()\u65b9\u6cd5\uff0c\u8ba1\u6570\u5668\u503c-1. \uff08\u5176\u5b9e\u5c31\u662fPV\u64cd\u4f5c\uff09\u3002 \u5982\u679c\u8ba1\u65f6\u5668\u503c\u4e3a0\uff0cacquire()\u65b9\u6cd5\u5c06\u7ebf\u7a0b\u6302\u8d77\uff0c\u76f4\u5230\u5176\u5b83\u7ebf\u7a0brelease\u4f7f\u5f97\u8ba1\u65f6\u5668\u503c+1\u4e86\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">Semaphore\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u5171\u4eab\u8bbf\u95ee\uff08value&gt;1\uff09\uff0c\u4e0d\u4ec5\u4ec5\u662f\u4e92\u65a5\u8bbf\u95ee(value=1)\u3002Event\u53ea\u80fd\u5b9e\u73b0\u4e92\u65a5\u8bbf\u95ee\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">threading\u6a21\u5757\u5176\u5b83\u91cd\u8981\u7c7b\u548c\u51fd\u6570<\/span><br \/>\n<span style=\"font-size:14px;\">threading.active_count(): \u76ee\u524d\u5b58\u6d3b\u7684\u7ebf\u7a0b\u6570\u91cf\u3002<\/span><br \/>\n<span style=\"font-size:14px;\">threading.current_thread(): \u8fd4\u56de\u5f53\u524d\u7684threading.Thread\u5bf9\u8c61<\/span><br \/>\n<span style=\"font-size:14px;\">threading.enumerate(): \u8fd4\u56de\u5f53\u524d\u5b58\u6d3b\u7684\u6240\u6709\u7ebf\u7a0b\u7684\u4e00\u5f20\u5217\u8868 list.<\/span><br \/>\n<span style=\"font-size:14px;\">threading.Timer: \u5b9a\u65f6\u5668\u7c7b\u3002\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e3athreading.Timer(interval, function, args=[], kwargs={}) interval\u662f\u5b9a\u65f6\u95f4\u9694\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0cfunction\u662f\u56de\u8c03\u51fd\u6570\uff0cargs\u662f\u4f20\u7ed9function\u7684\u53c2\u6570\u5217\u8868\u3002\u5373\uff0cinterval\u79d2\u540e\uff0c\u8c03\u7528\u4e00\u6b21function\uff0cfunction\u7684\u53c2\u6570\u4e3aargs. \u6ce8\u610ffunction\u53ea\u4f1a\u8c03\u7528\u4e00\u6b21\uff0c\u4e0d\u662f\u6bcf\u9694interval\u79d2\u3002<\/span>\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<strong><span style=\"font-size:14px;\">\u53c2\u8003\u8d44\u6599<\/span><\/strong>\n<\/p>\n<p>\n\t<span style=\"font-size:14px;\">python\u5b98\u65b9\u6587\u6863:&nbsp; thread \u2014 Multiple threads of control<\/span><br \/>\n<span style=\"font-size:14px;\">python\u5b98\u65b9\u6587\u6863\uff1athreading \u2014 Higher-level threading interface<\/span><br \/>\n<a href=\"http:\/\/www.cnblogs.com\/tqsummer\/archive\/2011\/01\/25\/1944771.html\"><span style=\"font-size:14px;\">http:\/\/www.cnblogs.com\/tqsummer\/archive\/2011\/01\/25\/1944771.html<\/span><\/a><br \/>\n<span style=\"font-size:14px;\">Python\u6a21\u5757\u5b66\u4e60 \u2014- threading \u591a\u7ebf\u7a0b\u63a7\u5236\u548c\u5904\u7406<\/span>\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t<span style=\"font-size:14px;\">Python\u4e2d\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u5e95\u5c42\uff08low-level\uff09\u7684thread\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u9ad8\u5c42(high-level)\u7684threading. \u4e24\u8005\u90fd\u53ef\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0cthread\u6a21\u5757\u4f7f\u7528\u7684\u662f\u4f4e\u7ea7\u7684\u539f\u8bed\uff0cthreading\u6a21\u5757\u57fa\u4e8ethread\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9ad8\u5c42\u7684API\uff0c\u66f4\u7b80\u5355\u6613\u7528\uff0c\u6240\u4ee5\u63a8\u8350\u4f7f\u7528threading\u6a21\u5757\u3002<\/span><br \/>\n<strong><span style=\"font-size:14px;\">\u6ce8\u610f<\/span><\/strong><span style=\"font-size:14px;\">\uff1a\u5728Python\u4e2d\uff0c\u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u662f\u4e0d\u5408\u7406\u7684\u3002\u56e0\u4e3aPyt&#8230;<\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[334],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65 - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=154\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65 - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Python\u4e2d\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u5e95\u5c42\uff08low-level\uff09\u7684thread\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u9ad8\u5c42(high-level)\u7684threading. \u4e24\u8005\u90fd\u53ef\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0cthread\u6a21\u5757\u4f7f\u7528\u7684\u662f\u4f4e\u7ea7\u7684\u539f\u8bed\uff0cthreading\u6a21\u5757\u57fa\u4e8ethread\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9ad8\u5c42\u7684API\uff0c\u66f4\u7b80\u5355\u6613\u7528\uff0c\u6240\u4ee5\u63a8\u8350\u4f7f\u7528threading\u6a21\u5757\u3002 \u6ce8\u610f\uff1a\u5728Python\u4e2d\uff0c\u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u662f\u4e0d\u5408\u7406\u7684\u3002\u56e0\u4e3aPyt...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=154\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-24T02:10:46+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=154#webpage\",\"url\":\"http:\/\/weizn.net\/?p=154\",\"name\":\"[\\u8f6c]Python\\u591a\\u7ebf\\u7a0b\\u548c\\u7ebf\\u7a0b\\u540c\\u6b65 - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2015-12-24T02:10:46+00:00\",\"dateModified\":\"2015-12-24T02:10:46+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=154#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=154\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=154#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\\u8f6c]Python\\u591a\\u7ebf\\u7a0b\\u548c\\u7ebf\\u7a0b\\u540c\\u6b65\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=154#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=154#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"[\\u8f6c]Python\\u591a\\u7ebf\\u7a0b\\u548c\\u7ebf\\u7a0b\\u540c\\u6b65\",\"datePublished\":\"2015-12-24T02:10:46+00:00\",\"dateModified\":\"2015-12-24T02:10:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=154#webpage\"},\"wordCount\":322,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=154#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65 - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=154","og_locale":"zh_CN","og_type":"article","og_title":"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65 - Wayne&#039;s Blog","og_description":"Python\u4e2d\u63d0\u4f9b\u4e86\u4e24\u4e2a\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u5e95\u5c42\uff08low-level\uff09\u7684thread\u6a21\u5757\uff0c\u4e00\u4e2a\u662f\u9ad8\u5c42(high-level)\u7684threading. \u4e24\u8005\u90fd\u53ef\u5b9e\u73b0\u591a\u7ebf\u7a0b\uff0cthread\u6a21\u5757\u4f7f\u7528\u7684\u662f\u4f4e\u7ea7\u7684\u539f\u8bed\uff0cthreading\u6a21\u5757\u57fa\u4e8ethread\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u9ad8\u5c42\u7684API\uff0c\u66f4\u7b80\u5355\u6613\u7528\uff0c\u6240\u4ee5\u63a8\u8350\u4f7f\u7528threading\u6a21\u5757\u3002 \u6ce8\u610f\uff1a\u5728Python\u4e2d\uff0c\u5982\u679c\u8bd5\u56fe\u901a\u8fc7\u591a\u7ebf\u7a0b\u6765\u63d0\u9ad8\u7a0b\u5e8f\u7684\u901f\u5ea6\u662f\u4e0d\u5408\u7406\u7684\u3002\u56e0\u4e3aPyt...","og_url":"http:\/\/weizn.net\/?p=154","og_site_name":"Wayne&#039;s Blog","article_published_time":"2015-12-24T02:10:46+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=154#webpage","url":"http:\/\/weizn.net\/?p=154","name":"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65 - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2015-12-24T02:10:46+00:00","dateModified":"2015-12-24T02:10:46+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=154#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=154"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=154#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=154#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=154#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"[\u8f6c]Python\u591a\u7ebf\u7a0b\u548c\u7ebf\u7a0b\u540c\u6b65","datePublished":"2015-12-24T02:10:46+00:00","dateModified":"2015-12-24T02:10:46+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=154#webpage"},"wordCount":322,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["Python"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=154#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=154"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/154\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}