zoukankan      html  css  js  c++  java
  • python 多线程与多进程

    #多线程
    import unittest from threading import Thread def test_search(self,browser,usl): #to do return True if __name__ == '__main__': data = { "ie": "http://www.baidu.com", "firefox": "http://www.baidu.com", "chrome": "http://www.baidu.com" } # 构建线程 threads = [] for b, url in data.items(): t = Thread(target=test_search, args=(b, url)) threads.append(t) # 启动所有线程 for thr in threads: # 守护进程 thr.setDaemon(True) thr.start() for t in threads: # 等待线程结束 t.join()

      

    # 多进程
    
    from multiprocessing import Process
    
    def test_search(self,browser,usl):
        #to do
        return True
    
    
    
    if __name__ == '__main__':
        data = {
            "ie": "http://www.baidu.com",
            "firefox": "http://www.baidu.com",
            "chrome": "http://www.baidu.com"
        }
        
        # 构建进程
        pro_list = []
        for i in data.items():
            p = Process(target=test_search, args=(i,))
            p.start()
            pro_list.append(p)
        # 等待所有进程结束
        for p in pro_list:
            p.join()
        print("主进程结束!")

      注:t = Thread(target=test_search, args=(b, url)) 中,args参数如果只有一个,后面的逗号也不能少,少了,可能会出现问题,如,你需要传一个dict,但是程序会把dict每个key作为一个参数,这样就会提示参数多了

  • 相关阅读:
    构建之法阅读笔记02
    学习进度
    构建之法阅读笔记01
    小学生的四则运算题
    构建之法----速读问题
    软件工程概论作业一
    分数 任意输入
    JAVA异常
    大道至简——第七、八章读后感
    super 要点
  • 原文地址:https://www.cnblogs.com/ai594ai/p/6842526.html
Copyright © 2011-2022 走看看