multiprocessing
多进程multiprocessing模块和多线程threading模块的使用方式很类似,在CPU密集型的程序中多线程并不能达到高效运转的效果,为了发挥多核CPU的优势使用多进程更有效果
# -*- coding: utf-8 -*- import multiprocessing import requests from time import ctime import json def syc_email(userID, userName): """syc_email""" print("Start synchronizing %s %s" % ("email", ctime())) parameter = {"userId":userID,"userName":userName,"enterpriseId":"10330","flag":"sended"} request_own = requests.put("https://xxxxxx.leadscloud.com/mail/receiveSendedAndRubbishMail", data=parameter) data = request_own.json() print(json.dumps(data, indent=4, sort_keys=True, ensure_ascii=False) ) print("接口调用已经返回结果,本次同步结束") dicts = {'1263':'13810078954','1294':'13810327625','1223':'18515934978','1295':'13911154792'} threads = [] files = range(len(dicts)) for userID, userName in dicts.items(): mp = multiprocessing.Process(target = syc_email, args = (userID, userName)) threads.append(mp) if __name__ == '__main__': for p in files: threads[p].start() for p in files: threads[p].join() print('all end: %s' % ctime())
执行结果
PS C:\Users\Administrator\Desktop> python .\multipreocess.py Start synchronizing email Thu Mar 21 13:54:26 2019 Start synchronizing email Thu Mar 21 13:54:26 2019 Start synchronizing email Thu Mar 21 13:54:26 2019 Start synchronizing email Thu Mar 21 13:54:26 2019 { "code": 1, "data": 0, "msg": "成功" } 接口调用已经返回结果,本次同步结束 { "code": 1, "data": 0, "msg": "成功" } 接口调用已经返回结果,本次同步结束 { "code": 1, "data": 0, "msg": "成功" } 接口调用已经返回结果,本次同步结束 { "code": 1, "data": 0, "msg": "成功" } 接口调用已经返回结果,本次同步结束 all end: Thu Mar 21 14:03:22 2019
# -*- coding: utf-8 -*- from selenium import webdriver from time import sleep from time import ctime import multiprocessing def start_browser(browser, time): if browser == "chrome": print("starting chrome browser now! %s" % ctime()) # 控制台打印当前时间 chrome_driver = webdriver.Chrome() chrome_driver.get("http://www.baidu.com") sleep(time) chrome_driver.quit() elif browser == "firefox": print("starting firefox browser now! %s" % ctime()) # 控制台打印当前时间 fire_driver = webdriver.Firefox() fire_driver.get("http://www.baidu.com") sleep(time) fire_driver.quit() else: print("starting ie browser now! %s" %ctime()) # 控制台打印当前时间 ie_driver = webdriver.Ie() ie_driver.get("http://www.baidu.com") sleep(time) ie_driver.quit() # 定义字典参数 browser_dict = {"chrome": 3, "firefox": 4} # 定义空List用于存储进程 start_browser_processing = [] # 循环字典Key-Value,创建进程并加入到List中 for browser, time in browser_dict.items(): processing_browser = multiprocessing.Process(target=start_browser, args=(browser, time)) start_browser_processing.append(processing_browser) if __name__ == '__main__': for processing_browser in range(len(browser_dict)): start_browser_processing[processing_browser].start() for processing_browser in range(len(browser_dict)): start_browser_processing[processing_browser].join() print(u"全部结束 %s" % ctime())