對比Python測試線程和進程在網絡請求中的效率

測試線程和進程在網絡請求中的效率
測試代碼如下:

# -*- coding:utf-8 -*-
import urllib2
import sys
import gc
import datetime
import multiprocessing
import threading
import time

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)

sys.setdefaultencoding(default_encoding)


# network function
def network(thread_name):
    # type: (thread_name) -> 線程名
    try:
        url = 'https://www.google.com/search?q=那個'
        headers = {"User-Agent": "Mozilla/5.0"}
        req = urllib2.Request(url, headers=headers)
        res = urllib2.urlopen(req, timeout=5)
        res = res.read()
        # now = datetime.datetime.now()
        # print(now.second, now.microsecond, thread_name, len(res))
        pass
    except Exception as e:
        # print ('ip_arr_error:{}'.format(e))
        gc.collect()
        pass
    now = datetime.datetime.now()
    print(now.second, now.microsecond, thread_name)


# 順序執(zhí)行網絡請求
def start_normal():
    for i in range(5):
        thread_name = "normalName" + str(i)
        network(thread_name)
    print("Sub-normal done.")

# 在線程中網絡請求
def start_thread():
    for i in range(5):
        thread_name = "threadName" + str(i)
        t = threading.Thread(target=network(thread_name))
        t.start()
    # t.join()
    print("Sub-thread(es) done.")

# 在進程中網絡請求
def start_processing():
    for i in range(5):
        thread_name = "processingName" + str(i)
        p = multiprocessing.Process(args=(thread_name,), target=network)
        p.start()
    p.join()
    print("Sub-process(es) done.")

if __name__ == "__main__":
    start_normal()
    start_thread()
    start_processing()

執(zhí)行結果如下:

bogon:WCDBBook caobo56$ python networkTest.py 
(29, 325209, 'normalName0')
(34, 344441, 'normalName1')
(39, 361590, 'normalName2')
(44, 379991, 'normalName3')
(49, 400796, 'normalName4')
Sub-normal done.
(54, 421533, 'threadName0')
(59, 438291, 'threadName1')
(4, 459484, 'threadName2')
(9, 522963, 'threadName3')
(14, 544185, 'threadName4')
Sub-thread(es) done.
(19, 569651, 'processingName0')
(19, 571128, 'processingName4')
(19, 572648, 'processingName1')
(19, 573374, 'processingName2')
(19, 574236, 'processingName3')
Sub-process(es) done.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,153評論 25 708
  • 英語資料】英語自然拼讀法之你不知道的28條發(fā)音秘密! 2017-10-09 英語好教師 1. 字母q總是與u在一起...
    小綠植物閱讀 297評論 0 0
  • 追求美麗是我們的共同心愿,愛美之心人皆有之。其實兩句話都已經將大眾的心聲說的透徹明了,長相真很重要,因為大家都很...
    旭日而生閱讀 267評論 0 1

友情鏈接更多精彩內容