Python高级知识点学习(八)

简介: 线程同步 - condition介绍多线程中的另外一个重要点就是condition:条件变量。condition是python多线程编程中用于复杂线程间通信的一个锁 叫做条件变量。

线程同步 - condition介绍

多线程中的另外一个重要点就是condition:条件变量。
condition是python多线程编程中用于复杂线程间通信的一个锁 叫做条件变量。

cond = threading.Condition()

with self.cond:
     cond.notify()
     cond.wait()

condition有两层锁, 一把底层锁会在线程调用了wait方法的时候释放, 上面的锁会在每次调用wait的时候分配一把并放入到cond的等待队列中,等到notify方法的唤醒。

有关condition的详情请查阅资料。(这里作者自己暂时还没理清楚原理,见谅)

线程同步 - Semaphore 介绍

信号量,Semaphore。
Semaphore 是用于控制进入数量的锁,控制进入某段代码的线程数。

文件, 读、写, 写一般只是用于一个线程写,读可以允许有多个。

ThreadPoolExecutor线程池

多线程和多进程对比

  • 运算,耗cpu的操作,用多进程编程
  • 对于io操作来说, 使用多线程编程

由于进程切换代价要高于线程,所以能使用线程就不用进程。

耗费cpu的操作:

def fib(n):
    if n<=2:
        return 1
    return fib(n-1)+fib(n-2)


if __name__ == "__main__":
    with ThreadPoolExecutor(3) as executor:
        all_task = [executor.submit(fib, (num)) for num in range(1, 10)]
        start_time = time.time()
        for future in as_completed(all_task):
            data = future.result()
            print("exe result: {}".format(data))

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

模拟IO操作:

def random_sleep(n):
    time.sleep(n)
    return n


if __name__ == "__main__":
    with ProcessPoolExecutor(3) as executor:
        all_task = [executor.submit(random_sleep, (num)) for num in [2]*30]
        start_time = time.time()
        for future in as_completed(all_task):
            data = future.result()
            print("exe result: {}".format(data))

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

multiprocessing 多进程

使用os.fork创建子进程,fork只能用于linux/unix中。

import os
import time
# fork新建子进程 fork只能用于linux/unix中
pid = os.fork()
print("a")
if pid == 0:
  print('子进程id:{} ,父进程id是: {}.' .format(os.getpid(), os.getppid()))
else:
  print('我是父进程, 我fork出的子进程id是:{}.'.format(pid))

time.sleep(2)

运行结果:
a
我是父进程, 我fork出的子进程id是:3093.
a
子进程id:3093 ,父进程id是: 3092.

运行结果中,可以看到打印了两次a,因为在执行完pid = os.fork()这行代码后,就创建了一个子进程,且子进程把父进程中的数据原样拷贝了一份到自己的进程中,所以父进程中打印一次,子进程中又打印一次。

多进程编程:

import multiprocessing

# 多进程编程
import time
def get_html(n):
    time.sleep(n)
    print("sub_progress success")
    return n


if __name__ == "__main__":
    progress = multiprocessing.Process(target=get_html, args=(2,))
    print(progress.pid)
    progress.start()
    print(progress.pid)
    progress.join()
    print("main progress end")

使用进程池:

import multiprocessing

# 多进程编程
import time
def get_html(n):
    time.sleep(n)
    print("sub_progress success")
    return n


if __name__ == "__main__":
    # 使用进程池
    pool = multiprocessing.Pool(multiprocessing.cpu_count())
    result = pool.apply_async(get_html, args=(3,))

    # 等待所有任务完成
    pool.close()
    pool.join()

    print(result.get())

进程池另一种方法:

import multiprocessing

# 多进程编程
import time
def get_html(n):
    time.sleep(n)
    print("sub_progress success")
    return n


if __name__ == "__main__":
 
    # 使用进程池
    pool = multiprocessing.Pool(multiprocessing.cpu_count())
    for result in pool.imap_unordered(get_html, [1, 5, 3]):
        print("{} sleep success".format(result))

进程间通信 Queue、Pipe,Manager

共享全局变量不能适用于多进程编程,可以适用于多线程。

进程间通信和线程间通信有相同也有不同,不同点是之前在多线程中用的线程间通信的类和线程间同步的锁在多进程中是不能用的。

使用multiprocessing中的Queue实现进程通信
import time
from multiprocessing import Process, Queue, Pool, Manager, Pipe


def producer(queue):
    queue.put("a")
    time.sleep(2)

def consumer(queue):
    time.sleep(2)
    data = queue.get()
    print(data)

if __name__ == "__main__":
    queue = Queue(10)
    my_producer = Process(target=producer, args=(queue,))
    my_consumer = Process(target=consumer, args=(queue,))
    my_producer.start()
    my_consumer.start()
    my_producer.join()
    my_consumer.join()

一定要使用multiprocessing中的Queue,如果使用import queue这个queue是不行的。

pool中的进程间通信需要使用manager中的queue

multiprocessing中的queue不能用于pool进程池。
pool中的进程间通信需要使用manager中的queue

def producer(queue):
    queue.put("a")
    time.sleep(2)

def consumer(queue):
    time.sleep(2)
    data = queue.get()
    print(data)

if __name__ == "__main__":
    queue = Manager().Queue(10)
    pool = Pool(2)

    pool.apply_async(producer, args=(queue,))
    pool.apply_async(consumer, args=(queue,))

    pool.close()
    pool.join()
使用Manager,多进程修改同一变量:
def add_data(p_dict, key, value):
    p_dict[key] = value


if __name__ == "__main__":
    progress_dict = Manager().dict()
    from queue import PriorityQueue

    first_progress = Process(target=add_data, args=(progress_dict, "a", 22))
    second_progress = Process(target=add_data, args=(progress_dict, "b", 23))

    first_progress.start()
    second_progress.start()
    first_progress.join()
    second_progress.join()

    print(progress_dict)

可以看到两个进程对一个dict变量做值得填充,最终主进程中打印出了最终的dict。

通过pipe实现进程间通信:

pipe的性能高于queue。
pipe只能适用于两个进程。

def producer(pipe):
    pipe.send("a")

def consumer(pipe):
    print(pipe.recv())


if __name__ == "__main__":
    recevie_pipe, send_pipe = Pipe()
    #pipe只能适用于两个进程
    my_producer = Process(target=producer, args=(send_pipe, ))
    my_consumer = Process(target=consumer, args=(recevie_pipe,))

    my_producer.start()
    my_consumer.start()
    my_producer.join()
    my_consumer.join()

my_producer进程给my_consumer进程发送的a变量可以正常打印。

目录
相关文章
|
7天前
|
存储 Python
Python时间模块四大必备知识点
Python时间模块四大必备知识点
16 4
Python时间模块四大必备知识点
|
13天前
|
机器学习/深度学习 开发者 Python
Python 与 R 在机器学习入门中的学习曲线差异
【8月更文第6天】在机器学习领域,Python 和 R 是两种非常流行的编程语言。Python 以其简洁的语法和广泛的社区支持著称,而 R 则以其强大的统计功能和数据分析能力受到青睐。本文将探讨这两种语言在机器学习入门阶段的学习曲线差异,并通过构建一个简单的线性回归模型来比较它们的体验。
36 7
|
11天前
|
JSON API 开发者
Python学习Get方式通过商品 ID请求 获取拼多多商品详情数据接口
拼多多商品详情数据接口服务使开发者或商家能编程获取平台商品详情,涵盖标题、价格、销量等关键信息,助力市场分析与决策。使用前需注册开发者账号并获取API密钥;构造含商品ID等参数的请求URL后发送至API服务器;接口以JSON格式返回数据。应用场景包括商品销售分析、选品、品牌口碑挖掘及竞品分析,为商家提供强大数据支持。
|
10天前
|
算法 数据挖掘 大数据
深入学习Python的性能优化
【8月更文挑战第9天】深入学习Python性能优化涵盖设定明确目标、运用timeit与cProfile等工具诊断瓶颈、优化代码结构与算法、采用并行/并发技术、利用生成器与第三方库等策略。这是一个持续学习的过程,旨在全面提升代码效率与响应速度。
18 1
|
12天前
|
数据采集 人工智能 数据可视化
【2023年电工杯竞赛】B题 人工智能对大学生学习影响的评价 数学建模方案和python代码
本文介绍了2023年电工杯竞赛B题的数学建模方案和Python代码实现,详细阐述了如何分析调查问卷数据,建立评价指标体系,构建数学模型评估人工智能对大学生学习的影响,并提供了数据预处理、特征编码、可视化分析等代码示例。
20 0
【2023年电工杯竞赛】B题 人工智能对大学生学习影响的评价 数学建模方案和python代码
|
20天前
|
机器学习/深度学习 搜索推荐 TensorFlow
使用Python实现深度学习模型:智能教育与个性化学习
【7月更文挑战第29天】 使用Python实现深度学习模型:智能教育与个性化学习
89 9
|
16天前
|
机器学习/深度学习 人工智能 TensorFlow
神经网络不再是黑魔法!Python带你一步步拆解,让AI学习看得见
【8月更文挑战第3天】神经网络,曾被视为难以触及的黑魔法,现已在Python的助力下变得平易近人。以TensorFlow或PyTorch为“魔法杖”,仅需几行Python代码即可构建强大的AI模型。从零开始,我们将教导AI识别手写数字,利用经典的MNIST数据集。通过数据加载、预处理至模型训练与评估,每个步骤都如精心编排的舞蹈般清晰可见。随着训练深入,AI逐渐学会辨认每个数字,其学习过程直观展现。这不仅揭示了神经网络的奥秘,更证明了任何人都能借助Python创造AI奇迹,共同探索未来的无限可能。
23 2
|
21天前
|
Ubuntu IDE Linux
Python学习安装 Python
【7月更文挑战第26天】
25 3
|
21天前
|
IDE 数据可视化 安全
Python学习中设置开发环境
【7月更文挑战第26天】
18 2
|
22天前
|
数据采集 Java C语言
Python面向对象的高级动态可解释型脚本语言简介
Python是一种面向对象的高级动态可解释型脚本语言。
17 3