Python多线程

多线程

在单线程执行任务的时候,必须按照任务的安排逐一执行并且是按顺序执行,而当我们的任务并没有相互依赖前后关系时,使用多线程会大大提高执行效率,Python提供了threading模块,我们可以使用threading模块中的Thread函数进行多线程方式的执行任务

# -*- coding: utf-8 -*-
import threading
import requests
from time import ctime
import json



def syc_owen():
    """syc_owen"""

    print("Start synchronizing %s %s" % ("owen", ctime()))
    parameter = {"userId":"1263","userName":"13810078954","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("接口调用已经返回结果,本次同步结束")

def syc_grace():
    """syc_grace"""

    print("Start synchronizing %s %s" % ("grace", ctime()))
    parameter = {"userId":"1294","userName":"13810327625","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("接口调用已经返回结果,本次同步结束")

def syc_jsliang():
    """syc_jsliang"""
    
    print("Start synchronizing %s %s" % ("jsliang", ctime()))
    parameter = {"userId":"1223","userName":"18515934978","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("接口调用已经返回结果,本次同步结束")

def syc_lucian():
    """syc_lucian"""
    print("Start synchronizing %s %s" % ("lucian", ctime()))
    parameter = {"userId":"1295","userName":"13911154792","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("接口调用已经返回结果,本次同步结束")

def syc_jeanne():
    """syc_jeanne"""
    print("Start synchronizing %s %s" % ("jeanne", ctime()))
    parameter = {"userId":"1244","userName":"13810391489","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("接口调用已经返回结果,本次同步结束")


syc_email_threads = []  # 定义线程列表,然后逐一将线程放进列表最后加载线程列表执行
owen_thread = threading.Thread(target=syc_owen)
syc_email_threads.append(owen_thread)

grace_thread = threading.Thread(target=syc_grace)
syc_email_threads.append(grace_thread)

jsliang_thread = threading.Thread(target=syc_jsliang)
syc_email_threads.append(jsliang_thread)

lucian_thread = threading.Thread(target=syc_lucian)
syc_email_threads.append(lucian_thread)

jeanne_thread = threading.Thread(target=syc_jeanne)
syc_email_threads.append(jeanne_thread)

if __name__ == '__main__':
    for threademail in syc_email_threads:
        threademail.start() # 启动线程活动
    for threademail in syc_email_threads:
        threademail.join() # 等待线程终止

执行结果

Python 3.7.1 | packaged by conda-forge | (default, Mar 13 2019, 13:32:59) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 7.3.0 -- An enhanced Interactive Python.

In [1]: runfile('C:/Users/Administrator/Desktop/multithreadinginvoke_syc_email.py', wdir='C:/Users/Administrator/Desktop')
Start synchronizing owen Wed Mar 20 12:23:23 2019
Start synchronizing grace Wed Mar 20 12:23:23 2019
Start synchronizing jsliang Wed Mar 20 12:23:23 2019
Start synchronizing lucian Wed Mar 20 12:23:23 2019
Start synchronizing jeanne Wed Mar 20 12:23:23 2019
Start synchronizing leonard Wed Mar 20 12:23:23 2019
Start synchronizing abby Wed Mar 20 12:23:23 2019
Start synchronizing tilly Wed Mar 20 12:23:23 2019
Start synchronizing chexinni Wed Mar 20 12:23:23 2019
Start synchronizing kimi Wed Mar 20 12:23:23 2019
Start synchronizing wangjun Wed Mar 20 12:23:23 2019
Start synchronizing david Wed Mar 20 12:23:23 2019
Start synchronizing steven Wed Mar 20 12:23:23 2019
Start synchronizing Amy Wed Mar 20 12:23:23 2019
Start synchronizing wendy Wed Mar 20 12:23:23 2019
Start synchronizing Kayla Wed Mar 20 12:23:23 2019
Start synchronizing wangjing Wed Mar 20 12:23:23 2019
Start synchronizing cindy Wed Mar 20 12:23:23 2019
Start synchronizing davidpan Wed Mar 20 12:23:23 2019
Start synchronizing fabiana Wed Mar 20 12:23:23 2019
Start synchronizing alice Wed Mar 20 12:23:23 2019
Start synchronizing james Wed Mar 20 12:23:23 2019
Start synchronizing helen Wed Mar 20 12:23:23 2019
Start synchronizing yulia Wed Mar 20 12:23:23 2019
Start synchronizing richard Wed Mar 20 12:23:23 2019
Start synchronizing mila Wed Mar 20 12:23:23 2019
Start synchronizing nina Wed Mar 20 12:23:23 2019
Start synchronizing julie Wed Mar 20 12:23:23 2019
Start synchronizing chenshiyuan Wed Mar 20 12:23:23 2019
Start synchronizing pengshuai Wed Mar 20 12:23:23 2019
Start synchronizing yolanda Wed Mar 20 12:23:23 2019
Start synchronizing francis Wed Mar 20 12:23:23 2019
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
{
    "code": 1,
    "data": 0,
    "msg": "成功"
}
接口调用已经返回结果,本次同步结束
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 24 19:23:25 2019
@author: davieyang
"""

from selenium import webdriver
from time import sleep
from time import ctime
import threading


def start_chrome():  # 定义启动谷歌浏览器的方法
    print("starting chrome browser now! %s" % ctime())
    chrome_driver = webdriver.Chrome()
    chrome_driver.get("http://www.baidu.com")
    sleep(2)
    chrome_driver.quit()


def start_firefox():  # 定义启动火狐浏览器的方法
    print("starting firefox browser now! %s" % ctime())
    fire_driver = webdriver.Firefox()
    fire_driver.get("http://www.baidu.com")
    sleep(3)
    fire_driver.quit()


def start_ie():  # 定义启动IE浏览器的方法
    print("starting ie browser now! %s" % ctime())
    ie_driver = webdriver.Ie()
    ie_driver.get("http://www.baidu.com")
    sleep(5)
    ie_driver.quit()
    
    
threading_list = []  #创建一个空列表用于存储线程
# 定义一个执行start_chrome()方法的线程
chrome_thread = threading.Thread(target=start_chrome)
threading_list.append(chrome_thread)  # 将线程放到列表中

# 定义一个执行start_firefox()方法的线程
firefox_thread = threading.Thread(target=start_firefox)
threading_list.append(firefox_thread)  # 将线程放到列表中
'''
# 定义一个执行start_chrome()方法的线程
ie_thread = threading.Thread(target=start_ie)  
threading_list.append(ie_thread)  # 将线程放到列表中
'''

if __name__ == '__main__':
    for start_thread in threading_list:
        start_thread.start()
    for start_thread in threading_list:
        start_thread.join()
    print(u"全部结束 %s" % ctime())
            

 

 

你可能感兴趣的:(Python多线程)