{"id":2691,"date":"2023-09-06T18:06:47","date_gmt":"2023-09-06T10:06:47","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2691"},"modified":"2023-09-06T18:07:08","modified_gmt":"2023-09-06T10:07:08","slug":"2691","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2691.html","title":{"rendered":"Python\u63a7\u5236\u591a\u8fdb\u7a0b\u4e0e\u591a\u7ebf\u7a0b\u5e76\u53d1\u6570"},"content":{"rendered":"

\u4e00\u3001\u524d\u8a00<\/strong><\/p>\n

\u672c\u6765\u5199\u4e86\u811a\u672c\u7528\u4e8e\u66b4\u529b\u7834\u89e3\u5bc6\u7801\uff0c\u53ef\u662f1\u79d2\u949f\u5c1d\u8bd5\u4e00\u4e2a\u5bc6\u78012220000\u4e2a\u5bc6\u7801\u6211\u7684\u5929\uff0c\u60f3\u7528\u591a\u7ebf\u7a0b\u53ef\u662f\u53ea\u4f1a\u4e00\u4e2afor\u5168\u5f00\uff0c\u96be\u9053\u5f002220000\u4e2a\u7ebf\u7a0b\u5417\uff1f\u53ea\u597d\u5b66\u4e60\u63a7\u5236\u7ebf\u7a0b\u6570\u4e86\uff0c\u5b98\u65b9\u6587\u6863\u4e0d\u597d\u770b\uff0c\u89c9\u5f97\u7ed3\u6784\u4e0d\u591f\u6e05\u6670\uff0c\u7f51\u4e0a\u627e\u5f88\u591a\u6587\u7ae0\u4e5f\u90fd\u4e0d\u5f88\u6e05\u6670\uff0c\u53ea\u6709for\u5168\u5f00\u7ebf\u7a0b\uff0c\u6ca1\u6709\u63a7\u5236\u7ebf\u7a0b\u6570\u7684\u5177\u4f53\u8bf4\u660e\uff0c\u6700\u7ec8\u7ec8\u4e8e\u6839\u636e\u591a\u7bc7\u6587\u7ae0\u548c\u5b98\u65b9\u6587\u6863\u7b97\u662f\u641e\u660e\u767d\u57fa\u7840\u7684\u591a\u7ebf\u7a0b\u600e\u4e48\u5b9e\u73b0\u6cd5\u4e86\uff0c\u6015\u957f\u65f6\u95f4\u4e0d\u7528\u53c8\u5fd8\u8bb0\uff0c\u627e\u7740\u9ebb\u70e6\u5c31\u8d34\u8fd9\u4e86\uff0c\u8ddf\u6211\u4e00\u6837\u65b0\u624b\u4e5f\u53ef\u4ee5\u53c2\u7167\u53c2\u7167\u3002<\/p>\n

\u5148\u8bf4\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u7684\u533a\u522b\uff1a<\/strong><\/p>\n

    \n
  1. \n

    \u5730\u5740\u7a7a\u95f4:\u8fdb\u7a0b\u5185\u7684\u4e00\u4e2a\u6267\u884c\u5355\u5143;\u8fdb\u7a0b\u81f3\u5c11\u6709\u4e00\u4e2a\u7ebf\u7a0b;\u5b83\u4eec\u5171\u4eab\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4;\u800c\u8fdb\u7a0b\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4;<\/p>\n<\/li>\n

  2. \n

    \u8d44\u6e90\u62e5\u6709:\u8fdb\u7a0b\u662f\u8d44\u6e90\u5206\u914d\u548c\u62e5\u6709\u7684\u5355\u4f4d,\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u7684\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90<\/p>\n<\/li>\n

  3. \n

    \u7ebf\u7a0b\u662f\u5904\u7406\u5668\u8c03\u5ea6\u7684\u57fa\u672c\u5355\u4f4d,\u4f46\u8fdb\u7a0b\u4e0d\u662f.<\/p>\n<\/li>\n

  4. \n

    \u4e8c\u8005\u5747\u53ef\u5e76\u53d1\u6267\u884c.<\/p>\n<\/li>\n<\/ol>\n

    \u4e0d\u80fd\u7406\u89e3\u7684\u8bdd\u7b80\u5355\u6253\u6bd4\u65b9\u5c31\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5c31\u50cf\u4e00\u4e2a\u7a0b\u5e8f\u4e00\u6837\uff0c\u5e76\u53d1\u4e92\u4e0d\u5e72\u6270\u3002\u4e00\u4e2a\u8fdb\u7a0b\u9760\u4e00\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u6267\u884c\u5904\u7406\uff0c\u5e76\u53d1\u7684\u7ebf\u7a0b\u662fcpu\u5728\u4e0d\u505c\u7684\u6765\u56de\u5207\u6362\u6267\u884c\uff0c\u5f53\u7136\u662f\u5feb\u5230\u4f60\u611f\u89c9\u4e0d\u51fa\u7684\u3002<\/p>\n

    \u62ff\u4e0a\u9762\u6211\u9047\u5230\u7684\u56f0\u96be\u6765\u8bf4\u5427\uff0c\u5927\u91cf\u7684\u6570\u636e\u9700\u8981\u6267\u884c\u76f8\u540c\u7684\u5904\u7406\uff0c\u4e00\u4e2a\u64cd\u4f5c\u4e2d\u95f4\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u7b49\u5f85\u65f6\u95f4\uff0c\u4e00\u4e2a\u4e00\u4e2a\u6267\u884c\u6d6a\u8d39\u5927\u91cf\u65f6\u95f4\uff0c\u90a3\u4e48\u5c31\u540c\u65f6\u6267\u884c\u5427\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e24\u79cd\u5e76\u884c\u529e\u6cd5\uff1a<\/p>\n

    \u8fdb\u7a0b\u5e76\u884c\u6216\u8005\u7ebf\u7a0b\u5e76\u884c<\/strong><\/p>\n

    \u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u8981\u770b\u60c5\u51b5\uff0c\u4e0d\u662f\u7edd\u5bf9\u7684\uff0c\u5728\u6b64\u4e0d\u8ba8\u8bba\u8fd9\u4e2a\uff0c\u8fd9\u5f15\u51fa\u4e0b\u9762\u4e24\u79cdPython\u5e76\u884c\u5904\u7406\u65b9\u6cd5\uff08\u6ce8\u91ca\u611f\u89c9\u5f88\u6e05\u6670\u8be6\u7ec6\u4e86\uff0c\u4e0d\u518d\u591a\u8bf4\uff09<\/p>\n

    \u4e8c\u3001\u8fdb\u7a0b\u5904\u7406\u65b9\u6cd5<\/strong><\/p>\n

    #coding:utf-8\r\nimport random\r\nfrom time import sleep\r\nimport sys\r\nimport multiprocessing\r\nimport os\r\n#\r\n#\u9700\u6c42\u5206\u6790\uff1a\u6709\u5927\u6279\u91cf\u6570\u636e\u9700\u8981\u6267\u884c\uff0c\u800c\u4e14\u662f\u91cd\u590d\u4e00\u4e2a\u51fd\u6570\u64cd\u4f5c\uff08\u4f8b\u5982\u7206\u7834\u5bc6\u7801\uff09\uff0c\u5982\u679c\u5168\u90e8\u5f00\u59cb\u7ebf\u7a0b\u6570N\u591a\uff0c\u8fd9\u91cc\u63a7\u5236\u4f4f\u7ebf\u7a0b\u6570m\u4e2a\u5e76\u884c\u6267\u884c\uff0c\u5176\u4ed6\u7b49\u5f85\r\n#\r\nlock=multiprocessing.Lock()#\u4e00\u4e2a\u9501\r\ndef a(x):#\u6a21\u62df\u9700\u8981\u91cd\u590d\u6267\u884c\u7684\u51fd\u6570\r\n  lock.acquire()#\u8f93\u51fa\u65f6\u5019\u4e0a\u9501\uff0c\u5426\u5219\u8fdb\u7a0b\u540c\u65f6\u8f93\u51fa\u65f6\u5019\u4f1a\u6df7\u4e71\uff0c\u4e0d\u53ef\u8bfb\r\n  print '\u5f00\u59cb\u8fdb\u7a0b\uff1a',os.getpid(),'\u6a21\u62df\u8fdb\u7a0b\u65f6\u95f4:',x\r\n  lock.release()\r\n  \r\n  sleep(x)#\u6a21\u62df\u6267\u884c\u64cd\u4f5c\r\n  \r\n  lock.acquire()\r\n  print '\u7ed3\u675f\u8fdb\u7a0b\uff1a',os.getpid(),'\u9884\u6d4b\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u542f\u52a8\u4f1a\u4f7f\u7528\u8be5\u8fdb\u7a0b\u53f7'\r\n  lock.release()\r\nlist=[]\r\nfor i in range(10):#\u4ea7\u751f\u4e00\u4e2a\u968f\u673a\u6570\u6570\u7ec4\uff0c\u6a21\u62df\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u9700\u8981\u7684\u8f93\u5165\uff0c\u8fd9\u91cc\u6a21\u62df\u603b\u5171\u670910\u7ec4\u9700\u8981\u5904\u7406\r\n  list.append(random.randint(1,10))\r\n  \r\npool=multiprocessing.Pool(processes=3)#\u9650\u5236\u5e76\u884c\u8fdb\u7a0b\u6570\u4e3a3\r\npool.map(a,list)#\u521b\u5efa\u8fdb\u7a0b\u6c60\uff0c\u8c03\u7528\u51fd\u6570a\uff0c\u4f20\u5165\u53c2\u6570\u4e3alist,\u6b64\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61,\u56e0\u4e3amap\u662f\u5728\u8fed\u4ee3\u521b\u5efa\u6bcf\u4e2a\u8fdb\u7a0b<\/pre>\n

    \u8f93\u51fa\uff1a<\/span><\/p>\n

    \"Python\u63a7\u5236\u591a\u8fdb\u7a0b\u4e0e\u591a\u7ebf\u7a0b\u5e76\u53d1\u6570\"<\/p>\n

    \u4e09\u3001\u7ebf\u7a0b\u5904\u7406\u65b9\u6cd5\uff1a<\/span><\/p>\n

    #coding:utf-8\r\nimport threading\r\nimport random\r\nimport Queue\r\nfrom time import sleep\r\nimport sys\r\n#\r\n#\u9700\u6c42\u5206\u6790\uff1a\u6709\u5927\u6279\u91cf\u6570\u636e\u9700\u8981\u6267\u884c\uff0c\u800c\u4e14\u662f\u91cd\u590d\u4e00\u4e2a\u51fd\u6570\u64cd\u4f5c\uff08\u4f8b\u5982\u7206\u7834\u5bc6\u7801\uff09\uff0c\u5982\u679c\u5168\u90e8\u5f00\u59cb\u7ebf\u7a0b\u6570N\u591a\uff0c\u8fd9\u91cc\u63a7\u5236\u4f4f\u7ebf\u7a0b\u6570m\u4e2a\u5e76\u884c\u6267\u884c\uff0c\u5176\u4ed6\u7b49\u5f85\r\n#\r\n#\u7ee7\u627f\u4e00\u4e2aThread\u7c7b\uff0c\u5728run\u65b9\u6cd5\u4e2d\u8fdb\u884c\u9700\u8981\u91cd\u590d\u7684\u5355\u4e2a\u51fd\u6570\u64cd\u4f5c\r\nclass Test(threading.Thread):\r\n  def __init__(self,queue,lock,num):\r\n    #\u4f20\u9012\u4e00\u4e2a\u961f\u5217queue\u548c\u7ebf\u7a0b\u9501\uff0c\u5e76\u884c\u6570\r\n    threading.Thread.__init__(self)\r\n    self.queue=queue\r\n    self.lock=lock\r\n    self.num=num\r\n  def run(self):\r\n    #while True:#\u4e0d\u4f7f\u7528threading.Semaphore\uff0c\u76f4\u63a5\u5f00\u59cb\u6240\u6709\u7ebf\u7a0b\uff0c\u7a0b\u5e8f\u6267\u884c\u5b8c\u6bd5\u7ebf\u7a0b\u90fd\u8fd8\u4e0d\u6b7b\uff0c\u6700\u540e\u7684print threading.enumerate()\u53ef\u4ee5\u770b\u51fa\r\n    with self.num:#\u540c\u65f6\u5e76\u884c\u6307\u5b9a\u7684\u7ebf\u7a0b\u6570\u91cf\uff0c\u6267\u884c\u5b8c\u6bd5\u4e00\u4e2a\u5219\u6b7b\u6389\u4e00\u4e2a\u7ebf\u7a0b\r\n      #\u4ee5\u4e0b\u4e3a\u9700\u8981\u91cd\u590d\u7684\u5355\u6b21\u51fd\u6570\u64cd\u4f5c\r\n      n=self.queue.get()#\u7b49\u5f85\u961f\u5217\u8fdb\u5165\r\n      lock.acquire()#\u9501\u4f4f\u7ebf\u7a0b\uff0c\u9632\u6b62\u540c\u65f6\u8f93\u51fa\u9020\u6210\u6df7\u4e71\r\n      print '\u5f00\u59cb\u4e00\u4e2a\u7ebf\u7a0b\uff1a',self.name,'\u6a21\u62df\u7684\u6267\u884c\u65f6\u95f4\uff1a',n\r\n      print '\u961f\u5217\u5269\u4f59\uff1a',queue.qsize()\r\n      print threading.enumerate()\r\n      lock.release()\r\n      sleep(n)#\u6267\u884c\u5355\u6b21\u64cd\u4f5c\uff0c\u8fd9\u91ccsleep\u6a21\u62df\u6267\u884c\u8fc7\u7a0b\r\n      self.queue.task_done()#\u53d1\u51fa\u6b64\u961f\u5217\u5b8c\u6210\u4fe1\u53f7\r\nthreads=[]\r\nqueue=Queue.Queue()\r\nlock=threading.Lock()\r\nnum=threading.Semaphore(3)#\u8bbe\u7f6e\u540c\u65f6\u6267\u884c\u7684\u7ebf\u7a0b\u6570\u4e3a3\uff0c\u5176\u4ed6\u7b49\u5f85\u6267\u884c\r\n#\u542f\u52a8\u6240\u6709\u7ebf\u7a0b\r\nfor i in range(10):#\u603b\u5171\u9700\u8981\u6267\u884c\u7684\u6b21\u6570\r\n  t=Test(queue,lock,num)\r\n  t.start()\r\n  threads.append(t)\r\n  #\u5427\u961f\u5217\u4f20\u5165\u7ebf\u7a0b\uff0c\u662frun\u7ed3\u675f\u7b49\u5f85\u5f00\u59cb\u6267\u884c\uff0c\u653e\u4e0b\u9762\u5355\u72ec\u4e00\u4e2afor\u4e5f\u884c\uff0c\u8fd9\u91cc\u5c11\u4e2a\u5faa\u73af\u5427\r\n  n=random.randint(1,10)\r\n  queue.put(n)#\u6a21\u62df\u6267\u884c\u51fd\u6570\u7684\u9010\u4e2a\u4e0d\u540c\u8f93\u5165\r\n#\u5427\u961f\u5217\u4f20\u5165\u7ebf\u7a0b\uff0c\u662frun\u7ed3\u675f\u7b49\u5f85\u5f00\u59cb\u6267\u884c\r\n#for t in threads:\r\n#  n=random.randint(1,10)\r\n#  queue.put(n)\r\n#\u7b49\u5f85\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\r\nfor t in threads:\r\n  t.join()\r\nqueue.join()#\u7b49\u5f85\u961f\u5217\u6267\u884c\u5b8c\u6bd5\u624d\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u4e0b\u9762\u8bed\u53e5\u4f1a\u5728\u7ebf\u7a0b\u672a\u63a5\u53d7\u5c31\u5f00\u59cb\u6267\u884c\r\nprint '\u6240\u6709\u6267\u884c\u5b8c\u6bd5'\r\nprint threading.active_count()\r\nprint threading.enumerate()<\/pre>\n

    \u8f93\u51fa\uff1a<\/span><\/p>\n

    \"Python\u63a7\u5236\u591a\u8fdb\u7a0b\u4e0e\u591a\u7ebf\u7a0b\u5e76\u53d1\u6570\"<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

    \u4e00\u3001\u524d\u8a00 \u672c\u6765\u5199\u4e86\u811a\u672c\u7528\u4e8e\u66b4\u529b\u7834\u89e3\u5bc6\u7801\uff0c\u53ef\u662f1\u79d2\u949f\u5c1d\u8bd5\u4e00\u4e2a\u5bc6\u78012220000\u4e2a\u5bc6\u7801\u6211\u7684\u5929\uff0c\u60f3\u7528\u591a\u7ebf\u7a0b\u53ef\u662f\u53ea\u4f1a\u4e00 […]<\/p>\n","protected":false},"author":1,"featured_media":2693,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2691","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2691"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2691"}],"version-history":[{"count":2,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2691\/revisions"}],"predecessor-version":[{"id":2699,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2691\/revisions\/2699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media\/2693"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}