3-6 多线程

版权声明:转载请标明出处,谢谢! https://blog.csdn.net/winfred_hua/article/details/88964972

多线程
Python的标准库提供了两个模块:_thread和threading,_thread是低级模块,threading是高级模块,对_thread进行了封装。绝大多数情况下,我们只需要使用threading这个高级模块。

1 实现多线程编程的方式一:通过Thread类实例化;
适用场景:少量代码,逻辑不复杂的情况;
实现代码:

import time
import threading


def get_detail_html(url):
    """获取网页详情"""
    print("in {}".format(url))
    print("get detail html started")

    time.sleep(2)
    print("get detail html end")


def get_detail_url(url):
    """获取网页url"""
    print("in {}".format(url))
    print("get detail url started")
    time.sleep(3)
    print("get detail url end")


if __name__ == "__main__":
    thread_1 = threading.Thread(target=get_detail_html, args=("thead_1", ))  # 创建进程
    thread_2 = threading.Thread(target=get_detail_url, args=("thead_2", ))  # 创建进程
    # thread_1.setDaemon(True)  # 设置为守护进程,主进程退出,子进程也退出
    # thread_2.setDaemon(True)  # 设置为守护进程,主进程退出,子进程也退出
    start_time = time.time()
    thread_1.start()  # 启动进程thread_1
    thread_2.start()  # 启动进程thread_2

    thread_1.join()  # 等待进程thread_1执行完
    thread_2.join()  # 等待进程thread_2执行完

    print("last time:{}".format(time.time() - start_time))

结果:

in thead_1
get detail html started
in thead_2
get detail url started
get detail html end
get detail url end
last time:3.001000165939331

注意:上述程序有3个线程:主线程,thread1,thread2;

2 实现多线程编程的方式二:通过继承Thread类来实现多线程;
适用场景:复杂的业务逻辑;

实现代码:

# 实现多线程编程的第二种方式
import time
import threading


class GetDetailHtml(threading.Thread):
    """获取页面html线程"""
    def __init__(self, name):
        """初始化父类"""
        super().__init__(name=name)

    def run(self):
        """重写run方法"""
        print("get detail html started")
        time.sleep(2)
        print("get detail html end")


class GetDetailUrl(threading.Thread):
    """获取页面url线程"""
    def __init__(self, name):
        """初始化父类"""
        super().__init__(name=name)

    def run(self):
        """重写run方法"""
        print("get url started")
        time.sleep(4)
        print("get url end")


if __name__ == "__main__":
    thread_1 = GetDetailHtml("get detail html")  # 创建进程
    thread_2 = GetDetailUrl("get detail url")  # 创建进程
    start_time = time.time()
    thread_1.start()  # 启动进程thread_1
    thread_2.start()  # 启动进程thread_2

    thread_1.join()  # 等待进程thread_1执行完
    thread_2.join()  # 等待进程thread_2执行完

    print("last time: {}".format(time.time()-start_time))

结果:

get detail html started
get url started
get detail html end
get url end
last time: 4.000999927520752

补充知识:
多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,因此,线程之间共享数据最大的危险在于多个线程同时改一个变量,把内容给改乱了。

参考:
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143192823818768cd506abbc94eb5916192364506fa5d000

猜你喜欢

转载自blog.csdn.net/winfred_hua/article/details/88964972
3-6