對(duì)比Python測(cè)試線程和進(jìn)程在網(wǎng)絡(luò)請(qǐng)求中的效率

測(cè)試線程和進(jìn)程在網(wǎng)絡(luò)請(qǐng)求中的效率
測(cè)試代碼如下:

# -*- 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=那個(gè)'
        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í)行網(wǎng)絡(luò)請(qǐng)求
def start_normal():
    for i in range(5):
        thread_name = "normalName" + str(i)
        network(thread_name)
    print("Sub-normal done.")

# 在線程中網(wǎng)絡(luò)請(qǐng)求
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.")

# 在進(jìn)程中網(wǎng)絡(luò)請(qǐng)求
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í)行結(jié)果如下:

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.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

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