• 进程和线程


    • 进程:一个程序 一个进程,操作系统分配资源(内存、线程)
    • 线程: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
  • 相关阅读:
    英语小三门
    2022最新解析最清晰 Java 系列面试题
    【100个 Unity实用技能】| Unity将本地图片文件显示到Image组件中 通用方法整理
    [附源码]计算机毕业设计实验室管理系统Springboot程序
    【视频】结构方程模型SEM分析心理学营销数据路径图可视化|数据分享
    crontab 无法激活、启动 pyenv failed to activate virtualenv
    pytorch中一维卷积,二维卷积,三维卷积,层次特征注意力
    工业互联网行至深水区,落地的路要怎么走?
    两种方式实现css取消页面鼠标双击选中文字或单击拖动选中文字的效果
    内网渗透之Socks代理简介
  • 原文地址:https://blog.csdn.net/qq_37755459/article/details/134513616