{"id":124713,"date":"2020-06-15T03:32:41","date_gmt":"2020-06-14T19:32:41","guid":{"rendered":"http:\/\/4563.org\/?p=124713"},"modified":"2020-06-15T03:32:41","modified_gmt":"2020-06-14T19:32:41","slug":"python-%e6%80%8e%e4%b9%88%e8%8e%b7%e5%8f%96%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%9a%84%e6%80%bb%e6%89%a7%e8%a1%8c%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=124713","title":{"rendered":"Python \u600e\u4e48\u83b7\u53d6\u591a\u7ebf\u7a0b\u7684\u603b\u6267\u884c\u65f6\u95f4??"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u600e\u4e48\u83b7\u53d6\u591a\u7ebf\u7a0b\u7684\u603b\u6267\u884c\u65f6\u95f4??               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : p1094358629 <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e00\u4e2a \u591a\u7ebf\u7a0b\u722c\u89c6\u9891\u7684 py \u811a\u672c,\u6211\u60f3\u770b\u4e0b\u603b\u7684\u6267\u884c\u65f6\u95f4,\u6c42\u5927\u4f6c\u5e2e\u5fd9\u770b\u770b???<\/p>\n<pre><code> # -*- coding: utf-8 -*- # @Author : \u5f90\u71da\u654f # @File : movie_multiple.py # @Project: python_spider # @CreateTime : 2020-06-22 10 # encoding utf-8 import requests from queue import Queue import threading import time  start_total_time = 0 end_total_time = 0 # \u521b\u5efa\u7ebf\u7a0b \u751f\u4ea7\u8005\u8d1f\u8d23\u83b7\u53d6 URL\uff0c\u5e76\u4e14\u89e3\u6790 URL class Procuder(threading.Thread):      def __init__(self, num_queue, ts_queue, *args, **kwargs):         super(Procuder, self).__init__(*args, **kwargs)         self.num_queue = num_queue         self.ts_queue = ts_queue      def run(self):         while True:             if self.num_queue.empty():                 # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\u5c31\uff0c\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\uff0c\u8df3\u51fa\u6b7b\u5faa\u73af\uff0c\u7ed3\u675f run                 print(\"Procuder \u961f\u5217\u4e3a\u7a7a\")                 break             url = self.num_queue.get()             self.download_ts(url)      def download_ts(self, url):         r = requests.get(url)         ret = r.content         # \u5012\u6570 8 \u4f4d,\u53d6\u5230\u6587\u4ef6\u540d         filename = url[-8:]         self.ts_queue.put((ret, filename))   # \u521b\u5efa\u7ebf\u7a0b \u6d88\u8d39\u8005 \u8d1f\u8d23\u4e0b\u8f7d class Consumer(threading.Thread):      def __init__(self, num_queue, ts_queue, *args, **kwargs):         super(Consumer, self).__init__(*args, **kwargs)         self.num_queue = num_queue         self.ts_queue = ts_queue      def run(self):         while True:             if self.ts_queue.empty() and self.num_queue.empty():                 # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\u5c31\uff0c\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\uff0c\u8df3\u51fa\u6b7b\u5faa\u73af\uff0c\u7ed3\u675f run                 print(\"Consumer \u961f\u5217\u4e3a\u7a7a\")                 end_total_time = time.time()                 break             start_time = time.time()             ret, filename = self.ts_queue.get()             # \u5c06 ts \u6587\u4ef6\u4fdd\u5b58\u5230\u6587\u4ef6\u5939\u91cc             with open('.\/mp4\/{}'.format(filename), 'wb') as f:                 f.write(ret)             end_time = time.time()             print('{}\u4e0b\u8f7d\u5b8c\u6210!,\u8017\u65f6:{}'.format(filename,end_time - start_time) )   def main():      # \u5b9a\u4e49\u7ebf\u7a0b     num_queue = Queue(500)     ts_queue = Queue(500)     # \u5148\u81ea\u5b9a\u4e49\u597d\u603b\u5171\u722c\u53d6\u7684\u6570\u91cf     # for i in range(470):     for i in range(30):         url = \" https:\/\/us8.wl-cdn.com\/hls\/20200619\/e1c5cc36967e6dac688b5e9c4006a567\/film_\" + '{0:05d}'.format(i) + \".ts\"         num_queue.put(url)      # time.sleep(1)     # \u5b9a\u4e49\u751f\u4ea7\u8005     for x in range(5):         print(\"Procuder\")         t = Procuder(num_queue, ts_queue)         t.start()     # \u5b9a\u4e49\u6d88\u8d39\u8005     for x in range(5):         print(\"Consumer\")         t = Consumer(num_queue, ts_queue)         t.start()    if __name__ == '__main__':     start_total_time = time.time()     lock = threading.RLock()     # \u52a0\u9501     lock.acquire()     try:         main()     finally:         # \u4fee\u6539\u5b8c\u6210\uff0c\u91ca\u653e\u9501         lock.release()         print(\"\u5b8c\u6210\u4e0b\u8f7d,\u603b\u8017\u65f6{}\".format(end_total_time - start_total_time))                    <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2178836\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Aliencn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8c03\u7528 main \u51fd\u6570\u524d\u540e\u53d6\u65f6\u95f4\u6233\uff0c\u7b97\u5dee\u503c\u4e0d\u5c31\u884c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178837\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jsisjs20130824 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u518d\u9000\u51fa\uff0c\u5b88\u62a4\u7ebf\u7a0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178838\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : marquina <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f04\u4e2a\u5b57\u5178\uff0c\u628a\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u8017\u65f6\u653e\u8fdb\u53bb\uff0c\u5b8c\u4e8b\u540e\u52a0\u8d77\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178839\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lithbitren <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u660e\u767d\u96be\u70b9\u5728\u54ea\uff0c\u5e0c\u671b\u77e5\u9053\u603b\u65f6\u95f4\u5c31\u76f4\u63a5\u6d4b main \u7684\u6267\u884c\u65f6\u95f4\uff0c\u9700\u8981\u77e5\u9053\u5404\u4e2a\u7ebf\u7a0b\u7684\u65f6\u95f4\uff0c\u5c31\u5728\u91cd\u8f7d\u7684 run \u51fd\u6570\u9996\u5c3e\u52a0\u4e0a\u8ba1\u65f6\u5c31\u884c\u4e86\uff0c\u7136\u540e\u5b58\u5b57\u5178\u5b58\u6570\u7ec4\u90fd\u53ef\u4ee5\u76f4\u63a5\u5b58\u3002io \u5bc6\u96c6\u7684\u65e0\u6240\u8c13\uff0c\u5982\u679c cpu \u5bc6\u96c6\u7684\u6700\u597d\u7ed9 run \u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u8fdb\u5165\u7ebf\u7a0b\u7684\u65f6\u95f4\u53c2\u6570\uff0c\u4e0d\u7136\u6709\u65f6\u5019\u53ef\u80fd\u4e00\u5f00\u7a0b\u5c31\u88ab\u6302\u8d77\u4e86\uff0c\u5bfc\u81f4\u65f6\u95f4\u6d4b\u91cf\u4e0d\u592a\u51c6\u786e\u3002<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u4e2a\u5faa\u73af\u4e5f\u6709\u70b9\u8ff7\uff0c\u5982\u679c\u5224\u65ad queue \u4e0d\u4e3a\u7a7a\uff0c\u5224\u65ad\u5b8c\u4ee5\u540e\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u5148\u628a\u961f\u5217\u6d88\u8d39\u7a7a\u4e86\uff0c\u90a3 get \u65b9\u6cd5\u5c31\u963b\u585e\u4e86\u554a\u3002<\/p>\n<p>\u5bf9\u4e8e\u8fd9\u79cd\u56fa\u5b9a\u6570\u91cf\u7684\u4efb\u52a1\uff0c\u65e2\u7136\u90fd\u7528 requests \u4e86\uff0c\u90a3\u76f4\u63a5\u4e0a grequests \u5c31\u5b8c\u4e8b\u4e86\uff0c\u6700\u591a\u91cd\u5199\u4e00\u4e0b\u91cc\u9762\u7684\u65b9\u6cd5\u52a0\u4e2a\u6210\u529f\u7684\u56de\u8c03\uff08\u539f\u751f\u5e93\u91cc\u9762\u53ea\u6709\u5931\u8d25\u56de\u8c03\uff09\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178840\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lithbitren <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             requests \u7684 asyncio \u7248 requests-async \u4e5f\u53ef\u4ee5\uff0c\u6216\u8005\u662f httpx \u4e5f\u884c\uff0c\u4e0d\u8fc7\u8981\u914d\u597d\u5b98\u65b9\u534f\u7a0b\uff0c\u6700\u597d\u628a py \u7684\u7248\u672c\u5f04\u65b0\u4e00\u70b9\uff0c\u4e0d\u7136\u6709\u65f6\u5019\u53ef\u80fd\u4f1a\u51fa\u73b0\u83ab\u540d\u5176\u5999\u7684 bug \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178841\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : p1094358629 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @marquina \u5e76\u884c\u7684\u54e6,\u603b\u548c\u5f53\u7136\u4e0d\u5bf9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178842\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : p1094358629 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Aliencn \u4e0d\u5bf9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178843\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : p1094358629 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lithbitren \u8fd0\u884c\u5c31\u76f4\u63a5 print \u603b\u65f6\u95f4\u4e86.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178844\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jugelizi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5 print \u6700\u540e\u4e00\u884c\u5c31\u662f\u7ed3\u675f\u65f6\u95f4 \u54c8\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178845\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : marquina <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @p1094358629 \u4e09\u4e2a\u7ebf\u7a0b\uff0c\u7ebf\u7a0b 1 \u6267\u884c 3 \u79d2\uff0c\u7ebf\u7a0b 2 \u6267\u884c 5 \u79d2\uff0c\u7ebf\u7a0b 3 \u6267\u884c 4 \u79d2\uff0c\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4e09\u4e2a\u7ebf\u7a0b\u7ed3\u675f\u3002\u65e0\u975e\u5c31\u4e24\u4e2a\u6267\u884c\u65f6\u95f4\uff0csum(3, 5, 4)\u3001max(3, 5, 4)\u3002\u5148\u641e\u6e05\u695a\u4f60\u8981\u7684\u662f\u54ea\u4e2a\u6267\u884c\u65f6\u95f4\u5427\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178846\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : momocraft <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ec0\u4e48\u53eb\u6267\u884c\u65f6\u95f4 \u5b9e\u9645\u5728 cpu \u8dd1\u7684\u65f6\u95f4? \u4efb\u52a1\u5b58\u5728\u7684\u65f6\u95f4?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2178847\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : p1094358629 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @marquina \u4f60\u8bf4\u7684\u4e5f\u5bf9,eg:\u6211\u662f\u628a 100 \u4e2a\u4efb\u52a1\u5206\u53d1\u7ed9 5 \u4e2a\u8fdb\u7a0b\u53bb\u6d88\u8d39,\u6211\u60f3\u77e5\u9053\u7684\u662f\u5e7f\u4e49\u4e0a,\u6267\u884c\u6700\u4e45\u7684\u7ebf\u7a0b,\u4e5f\u5c31\u662f\u603b\u7684\u6267\u884c\u65f6\u95f4<br \/>@marquina <br \/>@momocraft                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u600e\u4e48\u83b7\u53d6\u591a\u7ebf\u7a0b\u7684\u603b\u6267&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\/124713"}],"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=124713"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124713\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124713"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}