• 进程和线程


    • 进程:一个程序 一个进程,操作系统分配资源(内存、线程)
    • 线程:cpu运算调度的最小单位

    创建

    方法一

    from threading import Thread
    
    
    def thread_func(name):
        for i in range(10):
            print(name, i)
    # 主线程中创建了3个子线程
    if __name__ == '__main__':
        thread1 = Thread(target=thread_func, args=('张三',)) # 参数是元组
        thread2 = Thread(target=thread_func, args=('李四',)) # 参数是元组
        thread3 = Thread(target=thread_func, args=('王五',)) # 参数是元组
        thread1.start()
        thread2.start()
        thread3.start()
        print("主线程")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    方法二

    续承Thread类

    from threading import Thread
    
    class ThreadFunc(Thread):
        def __init__(self, name):
            super(ThreadFunc, self).__init__()
            self.name = name
        # 重写run函数
        def run(self):
            for i in range(10):
                print(self.name, i, sep="---")
    # 主线程中创建了3个子线程
    if __name__ == '__main__':
        thread1 = ThreadFunc('张三')
        thread2 = ThreadFunc('李四')
        thread3 = ThreadFunc('王五')
        thread1.start()
        thread2.start()
        thread3.start()
        print("主线程")
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    线程池

    无返回值

    from concurrent.futures import ThreadPoolExecutor
    
    def work_thread(name):
        for i in range(8):
            print(name)
    if __name__ == '__main__':
        with ThreadPoolExecutor(8) as thread_pool:
            for i in range(5):
                thread_pool.submit(work_thread, f"岳王{i}")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    有返回值

    • 返回值的顺序不确定,谁执行完,先返回谁
    import time
    from concurrent.futures import ThreadPoolExecutor
    
    def work_thread(name, t):
        time.sleep(t)
        # print("you can call me ", name)
        return name
    def func_return(outcome):
        print(outcome.result())
    if __name__ == '__main__':
        with ThreadPoolExecutor(8) as thread_pool:
            # 分别睡3秒、1秒、2秒
            thread_pool.submit(work_thread, "岳王",3).add_done_callback(func_return)
            thread_pool.submit(work_thread, "张飞",1).add_done_callback(func_return)
            thread_pool.submit(work_thread, "刘备",2).add_done_callback(func_return)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 返回值的顺序不确定。map返回值是生成器,按任务分发的顺序进行返回
    import time
    from concurrent.futures import ThreadPoolExecutor
    
    def work_thread(name, t):
        time.sleep(t)
        # print("you can call me ", name)
        return name
    def func_return(outcome):
        print(outcome.result())
    if __name__ == '__main__':
        with ThreadPoolExecutor(8) as thread_pool:
            # 分别睡3秒、1秒、2秒
            res = thread_pool.map(work_thread, ['岳王', '张飞', '刘备'], [3, 1, 2])
            for i in res:
                print(i)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    线程实例

    from concurrent.futures import ThreadPoolExecutor
    import requests
    from lxml import etree
    def get_data(page):
        data = {
            'limit': '20',
            'current': page,
            'pubDateStartTime':'',
            'pubDateEndTime':'',
            'prodPcatid': '1189',
            'prodCatid':'',
            'prodName':'',
        }
        url = '仅做格式http://www.xinfadi.com.cn/getPriceData.html未曾试用'
        res = requests.post(url, data=data).json()
        for i in res['list']:
            # print(i)
            one_row = i['prodCat']+','+i['prodPcat']+','+i['prodName']+','+i['lowPrice']+','+i['avgPrice']+','+i['highPrice']+','+i['specInfo']+','+i['place']+','+i['unitInfo']+','+i['pubDate']
            f.write(one_row)
            f.write('\n')
    f = open('price.csv', 'w', encoding='utf-8')
    if __name__ == '__main__':
        with ThreadPoolExecutor(8) as thread_pool:
            for page in range(1, 8):
                thread_pool.submit(get_data, page)
    
        # print(res['list'])
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    精品基于ssm的足球联赛管理系统的设计与实现vue
    【刷题记录⑧】Java工程师丨字节面试真题(二)
    非零基础自学Java (老师:韩顺平) 第7章 面向对象编程(基础部分) 7.3 成员方法传参机制
    详谈Web3与品牌如何推动下一轮牛市
    HIVE 表 DLL 基本操作(一)——第1关:Create/Alter/Drop 数据库
    计算机专业毕业设计项目推荐07-科研成果管理系统(JavaSpringBoot+Vue+Mysql)
    前端性能优化——采用高效的缓存策略提供静态资源
    clion出现createprocess error=193, %1 不是有效的 win32 应用程序
    华为云CDN,如何推动互联网行业健康发展?
    ECA-Net:深度卷积神经网络的高效通道注意力
  • 原文地址:https://blog.csdn.net/qq_37755459/article/details/134513616