#多线程
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作为一个参数,这样就会提示参数多了