美文网首页
对比Python测试线程和进程在网络请求中的效率

对比Python测试线程和进程在网络请求中的效率

作者: 曹波波 | 来源:发表于2017-11-13 09:02 被阅读15次

    测试线程和进程在网络请求中的效率
    测试代码如下:

    # -*- 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)
    
    
    # 顺序执行网络请求
    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()
    

    执行结果如下:

    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.
    
    

    相关文章

      网友评论

          本文标题:对比Python测试线程和进程在网络请求中的效率

          本文链接:https://www.haomeiwen.com/subject/bkybmxtx.html