首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >python 协程池和pool.map用法

python 协程池和pool.map用法

作者头像
py3study
发布于 2020-02-25 04:03:11
发布于 2020-02-25 04:03:11
3.7K00
代码可运行
举报
文章被收录于专栏:python3python3
运行总次数:0
代码可运行

一、问题描述

现在有一段代码,需要扫描一个网段内的ip地址,是否可以ping通。

执行起来效率太慢,需要使用协程。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import time
import signal
import subprocess
import gevent
import gevent.pool
from gevent import monkey;monkey.patch_all()


def custom_print(content,colour='white'):
    """
    写入日志文件
    :param content: 内容
    :param colour: 颜色
    :return: None
    """
    # 颜色代码
    colour_dict = {
        'red': 31,  # 红色
        'green': 32,  # 绿色
        'yellow': 33,  # 黄色
        'blue': 34,  # 蓝色
        'purple_red': 35,  # 紫红色
        'bluish_blue': 36, # 浅蓝色
        'white': 37,  # 白色
    }
    choice = colour_dict.get(colour)  # 选择颜色

    info = "\033[1;{};1m{}\033[0m".format(choice, content)
    print(info)


def execute_linux2(cmd, timeout=10, skip=False):
    """
    执行linux命令,返回list
    :param cmd: linux命令
    :param timeout: 超时时间,生产环境, 特别卡, 因此要3
    :param skip: 是否跳过超时限制
    :return: list
    """
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True,close_fds=True,preexec_fn=os.setsid)

    t_beginning = time.time()  # 开始时间
    while True:
        if p.poll() is not None:
            break
        seconds_passed = time.time() - t_beginning
        if not skip:
            if seconds_passed > timeout:
                # p.terminate()
                # p.kill()
                # raise TimeoutError(cmd, timeout)
                custom_print('错误, 命令: {},本地执行超时!'.format(cmd),"red")
                # 当shell=True时,只有os.killpg才能kill子进程
                try:
                    # time.sleep(1)
                    os.killpg(p.pid, signal.SIGUSR1)
                except Exception as e:
                    pass
                return False

    result = p.stdout.readlines()  # 结果输出列表
    return result


class NetworkTest(object):
    def __init__(self):
        self.flag_list = []

    def check_ping(self,ip):
        """
        检查ping
        :param ip: ip地址
        :return: none
        """
        cmd = "ping %s -c 2" % ip
        # print(cmd)
        # 本机执行命令
        res = execute_linux2(cmd,2)
        # print(res)
        if not res:
            custom_print("错误, 执行命令: {} 失败".format(cmd), "red")
            self.flag_list.append(False)
            return False

        res.pop()  # 删除最后一个元素
        last_row = res.pop().decode('utf-8').strip()  # 再次获取最后一行结果
        if not last_row:
            custom_print("错误,执行命令: {} 异常","red")
            self.flag_list.append(False)
            return False

        res = last_row.split()  # 切割结果
        # print(res,type(res),len(res))
        if len(res) <10:
            custom_print("错误,切割 ping 结果异常","red")
            self.flag_list.append(False)
            return False

        if res[5] == "0%":  # 判断丢包率
            custom_print("正常, ip: {} ping正常 丢包率0%".format(ip), "green")
        else:
            self.flag_list.append(False)
            custom_print("错误, ip: {} ping异常 丢包率100%".format(ip), "red")

    def main(self):
        """
        主程序
        :return:
        """
        for num in range(1, 256):
            ip = '192.168.10.{}'.format(num)
            self.check_ping(ip)


if __name__ == '__main__':
    startime = time.time()  # 开始时间

    NetworkTest().main()

    endtime = time.time()
    take_time = endtime - startime

    if take_time < 1:  # 判断不足1秒时
        take_time = 1  # 设置为1秒
    # 计算花费时间
    m, s = divmod(take_time, 60)
    h, m = divmod(m, 60)

    custom_print("本次花费时间 %02d:%02d:%02d" % (h, m, s),"green")

改造成,协程执行。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import time
import signal
import subprocess
import gevent
import gevent.pool
from gevent import monkey;monkey.patch_all()


def custom_print(content,colour='white'):
    """
    写入日志文件
    :param content: 内容
    :param colour: 颜色
    :return: None
    """
    # 颜色代码
    colour_dict = {
        'red': 31,  # 红色
        'green': 32,  # 绿色
        'yellow': 33,  # 黄色
        'blue': 34,  # 蓝色
        'purple_red': 35,  # 紫红色
        'bluish_blue': 36, # 浅蓝色
        'white': 37,  # 白色
    }
    choice = colour_dict.get(colour)  # 选择颜色

    info = "\033[1;{};1m{}\033[0m".format(choice, content)
    print(info)


def execute_linux2(cmd, timeout=10, skip=False):
    """
    执行linux命令,返回list
    :param cmd: linux命令
    :param timeout: 超时时间,生产环境, 特别卡, 因此要3
    :param skip: 是否跳过超时限制
    :return: list
    """
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True,close_fds=True,preexec_fn=os.setsid)

    t_beginning = time.time()  # 开始时间
    while True:
        if p.poll() is not None:
            break
        seconds_passed = time.time() - t_beginning
        if not skip:
            if seconds_passed > timeout:
                # p.terminate()
                # p.kill()
                # raise TimeoutError(cmd, timeout)
                custom_print('错误, 命令: {},本地执行超时!'.format(cmd),"red")
                # 当shell=True时,只有os.killpg才能kill子进程
                try:
                    # time.sleep(1)
                    os.killpg(p.pid, signal.SIGUSR1)
                except Exception as e:
                    pass
                return False

    result = p.stdout.readlines()  # 结果输出列表
    return result


class NetworkTest(object):
    def __init__(self):
        self.flag_list = []

    def check_ping(self,ip):
        """
        检查ping
        :param ip: ip地址
        :return: none
        """
        cmd = "ping %s -c 2" % ip
        # print(cmd)
        # 本机执行命令
        res = execute_linux2(cmd,2)
        # print(res)
        if not res:
            custom_print("错误, 执行命令: {} 失败".format(cmd), "red")
            self.flag_list.append(False)
            return False

        res.pop()  # 删除最后一个元素
        last_row = res.pop().decode('utf-8').strip()  # 再次获取最后一行结果
        if not last_row:
            custom_print("错误,执行命令: {} 异常","red")
            self.flag_list.append(False)
            return False

        res = last_row.split()  # 切割结果
        # print(res,type(res),len(res))
        if len(res) <10:
            custom_print("错误,切割 ping 结果异常","red")
            self.flag_list.append(False)
            return False

        if res[5] == "0%":  # 判断丢包率
            custom_print("正常, ip: {} ping正常 丢包率0%".format(ip), "green")
        else:
            self.flag_list.append(False)
            custom_print("错误, ip: {} ping异常 丢包率100%".format(ip), "red")

    def main(self):
        """
        主程序
        :return:
        """
        process_list = []
        for num in range(1, 256):
            ip = '192.168.10.{}'.format(num)
            # self.check_ping(ip)
            # 将任务加到列表中
            process_list.append(gevent.spawn(self.check_ping, ip))

        gevent.joinall(process_list)  # 等待所有协程结束


if __name__ == '__main__':
    startime = time.time()  # 开始时间

    NetworkTest().main()

    endtime = time.time()
    take_time = endtime - startime

    if take_time < 1:  # 判断不足1秒时
        take_time = 1  # 设置为1秒
    # 计算花费时间
    m, s = divmod(take_time, 60)
    h, m = divmod(m, 60)

    custom_print("本次花费时间 %02d:%02d:%02d" % (h, m, s),"green")

执行输出:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
...
错误, 命令: ping 192.168.10.250 -c 2,本地执行超时!
错误, 执行命令: ping 192.168.10.250 -c 2 失败
错误, 命令: ping 192.168.10.255 -c 2,本地执行超时!
错误, 执行命令: ping 192.168.10.255 -c 2 失败
本次花费时间 00:00:07

注意:切勿在windows系统中运行,否则会报错

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
AttributeError: module 'os' has no attribute 'setsid'

二、使用协程池

上面直接将所有任务加到列表中,然后一次性,全部异步执行。那么同一时刻,最多有多少任务执行呢?

不知道,可能有256个吧?

注意:如果这个一个很耗CPU的程序,可能会导致服务器,直接卡死。

那么,我们应该要限制它的并发数。这个时候,需要使用协程池,固定并发数。

比如:固定为100个

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import time
import signal
import subprocess
import gevent
import gevent.pool
from gevent import monkey;monkey.patch_all()


def custom_print(content,colour='white'):
    """
    写入日志文件
    :param content: 内容
    :param colour: 颜色
    :return: None
    """
    # 颜色代码
    colour_dict = {
        'red': 31,  # 红色
        'green': 32,  # 绿色
        'yellow': 33,  # 黄色
        'blue': 34,  # 蓝色
        'purple_red': 35,  # 紫红色
        'bluish_blue': 36, # 浅蓝色
        'white': 37,  # 白色
    }
    choice = colour_dict.get(colour)  # 选择颜色

    info = "\033[1;{};1m{}\033[0m".format(choice, content)
    print(info)


def execute_linux2(cmd, timeout=10, skip=False):
    """
    执行linux命令,返回list
    :param cmd: linux命令
    :param timeout: 超时时间,生产环境, 特别卡, 因此要3
    :param skip: 是否跳过超时限制
    :return: list
    """
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True,close_fds=True,preexec_fn=os.setsid)

    t_beginning = time.time()  # 开始时间
    while True:
        if p.poll() is not None:
            break
        seconds_passed = time.time() - t_beginning
        if not skip:
            if seconds_passed > timeout:
                # p.terminate()
                # p.kill()
                # raise TimeoutError(cmd, timeout)
                custom_print('错误, 命令: {},本地执行超时!'.format(cmd),"red")
                # 当shell=True时,只有os.killpg才能kill子进程
                try:
                    # time.sleep(1)
                    os.killpg(p.pid, signal.SIGUSR1)
                except Exception as e:
                    pass
                return False

    result = p.stdout.readlines()  # 结果输出列表
    return result


class NetworkTest(object):
    def __init__(self):
        self.flag_list = []

    def check_ping(self,ip):
        """
        检查ping
        :param ip: ip地址
        :return: none
        """
        cmd = "ping %s -c 2" % ip
        # print(cmd)
        # 本机执行命令
        res = execute_linux2(cmd,2)
        # print(res)
        if not res:
            custom_print("错误, 执行命令: {} 失败".format(cmd), "red")
            self.flag_list.append(False)
            return False

        res.pop()  # 删除最后一个元素
        last_row = res.pop().decode('utf-8').strip()  # 再次获取最后一行结果
        if not last_row:
            custom_print("错误,执行命令: {} 异常","red")
            self.flag_list.append(False)
            return False

        res = last_row.split()  # 切割结果
        # print(res,type(res),len(res))
        if len(res) <10:
            custom_print("错误,切割 ping 结果异常","red")
            self.flag_list.append(False)
            return False

        if res[5] == "0%":  # 判断丢包率
            custom_print("正常, ip: {} ping正常 丢包率0%".format(ip), "green")
        else:
            self.flag_list.append(False)
            custom_print("错误, ip: {} ping异常 丢包率100%".format(ip), "red")

    def main(self):
        """
        主程序
        :return:
        """
        process_list = []
        pool= gevent.pool.Pool(100)  # 协程池固定为100for num in range(1, 256):
            ip = '192.168.10.{}'.format(num)
            # self.check_ping(ip)
            # 将任务加到列表中
            process_list.append(pool.spawn(self.check_ping, ip))

        gevent.joinall(process_list)  # 等待所有协程结束


if __name__ == '__main__':
    startime = time.time()  # 开始时间

    NetworkTest().main()

    endtime = time.time()
    take_time = endtime - startime

    if take_time < 1:  # 判断不足1秒时
        take_time = 1  # 设置为1秒
    # 计算花费时间
    m, s = divmod(take_time, 60)
    h, m = divmod(m, 60)

    custom_print("本次花费时间 %02d:%02d:%02d" % (h, m, s),"green")

再次执行,效果如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
...
错误, 执行命令: ping 192.168.10.254 -c 2 失败
错误, 命令: ping 192.168.10.255 -c 2,本地执行超时!
错误, 执行命令: ping 192.168.10.255 -c 2 失败
本次花费时间 00:00:15

可以,发现花费的时间,明显要比上面慢了!

pool.map 单个参数

其实,还有一种写法,使用pool.map,语法如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pool.map(func,iterator)

比如:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pool.map(self.get_kernel, NODE_LIST)

注意:func是一个方法,iterator是一个迭代器。比如:list就是一个迭代器

使用map时,func只能接收一个参数。这个参数就是,遍历迭代器的每一个值。

使用map,完整代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import time
import signal
import subprocess
import gevent
import gevent.pool
from gevent import monkey;monkey.patch_all()


def custom_print(content,colour='white'):
    """
    写入日志文件
    :param content: 内容
    :param colour: 颜色
    :return: None
    """
    # 颜色代码
    colour_dict = {
        'red': 31,  # 红色
        'green': 32,  # 绿色
        'yellow': 33,  # 黄色
        'blue': 34,  # 蓝色
        'purple_red': 35,  # 紫红色
        'bluish_blue': 36, # 浅蓝色
        'white': 37,  # 白色
    }
    choice = colour_dict.get(colour)  # 选择颜色

    info = "\033[1;{};1m{}\033[0m".format(choice, content)
    print(info)


def execute_linux2(cmd, timeout=10, skip=False):
    """
    执行linux命令,返回list
    :param cmd: linux命令
    :param timeout: 超时时间,生产环境, 特别卡, 因此要3
    :param skip: 是否跳过超时限制
    :return: list
    """
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True,close_fds=True,preexec_fn=os.setsid)

    t_beginning = time.time()  # 开始时间
    while True:
        if p.poll() is not None:
            break
        seconds_passed = time.time() - t_beginning
        if not skip:
            if seconds_passed > timeout:
                # p.terminate()
                # p.kill()
                # raise TimeoutError(cmd, timeout)
                custom_print('错误, 命令: {},本地执行超时!'.format(cmd),"red")
                # 当shell=True时,只有os.killpg才能kill子进程
                try:
                    # time.sleep(1)
                    os.killpg(p.pid, signal.SIGUSR1)
                except Exception as e:
                    pass
                return False

    result = p.stdout.readlines()  # 结果输出列表
    return result


class NetworkTest(object):
    def __init__(self):
        self.flag_list = []

    def check_ping(self,ip):
        """
        检查ping
        :param ip: ip地址
        :return: none
        """
        cmd = "ping %s -c 2" % ip
        # print(cmd)
        # 本机执行命令
        res = execute_linux2(cmd,2)
        # print(res)
        if not res:
            custom_print("错误, 执行命令: {} 失败".format(cmd), "red")
            self.flag_list.append(False)
            return False

        res.pop()  # 删除最后一个元素
        last_row = res.pop().decode('utf-8').strip()  # 再次获取最后一行结果
        if not last_row:
            custom_print("错误,执行命令: {} 异常","red")
            self.flag_list.append(False)
            return False

        res = last_row.split()  # 切割结果
        # print(res,type(res),len(res))
        if len(res) <10:
            custom_print("错误,切割 ping 结果异常","red")
            self.flag_list.append(False)
            return False

        if res[5] == "0%":  # 判断丢包率
            custom_print("正常, ip: {} ping正常 丢包率0%".format(ip), "green")
        else:
            self.flag_list.append(False)
            custom_print("错误, ip: {} ping异常 丢包率100%".format(ip), "red")

    def main(self):
        """
        主程序
        :return:
        """
        pool= gevent.pool.Pool(100)  # 协程池固定为100个
        ip_list = ["192.168.10.{}".format(i) for i in range(1, 256)]
        # 使用pool.map,语法:pool.map(func,iterator)
        pool.map(self.check_ping, ip_list)


if __name__ == '__main__':
    startime = time.time()  # 开始时间

    NetworkTest().main()

    endtime = time.time()
    take_time = endtime - startime

    if take_time < 1:  # 判断不足1秒时
        take_time = 1  # 设置为1秒
    # 计算花费时间
    m, s = divmod(take_time, 60)
    h, m = divmod(m, 60)

    custom_print("本次花费时间 %02d:%02d:%02d" % (h, m, s),"green")

注意:方法只有一个参数的情况下,使用pool.map,一行就可以搞定。这样看起来,比较精简!

pool.map 多参数

如果方法,有多个参数,需要借用偏函数实现。

完整代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#!/usr/bin/env python3
# coding: utf-8

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
import time
import signal
import subprocess
import gevent
import gevent.pool
from gevent import monkey;monkey.patch_all()
from functools import partial

def custom_print(content,colour='white'):
    """
    写入日志文件
    :param content: 内容
    :param colour: 颜色
    :return: None
    """
    # 颜色代码
    colour_dict = {
        'red': 31,  # 红色
        'green': 32,  # 绿色
        'yellow': 33,  # 黄色
        'blue': 34,  # 蓝色
        'purple_red': 35,  # 紫红色
        'bluish_blue': 36, # 浅蓝色
        'white': 37,  # 白色
    }
    choice = colour_dict.get(colour)  # 选择颜色

    info = "\033[1;{};1m{}\033[0m".format(choice, content)
    print(info)


def execute_linux2(cmd, timeout=10, skip=False):
    """
    执行linux命令,返回list
    :param cmd: linux命令
    :param timeout: 超时时间,生产环境, 特别卡, 因此要3
    :param skip: 是否跳过超时限制
    :return: list
    """
    p = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE,shell=True,close_fds=True,preexec_fn=os.setsid)

    t_beginning = time.time()  # 开始时间
    while True:
        if p.poll() is not None:
            break
        seconds_passed = time.time() - t_beginning
        if not skip:
            if seconds_passed > timeout:
                # p.terminate()
                # p.kill()
                # raise TimeoutError(cmd, timeout)
                custom_print('错误, 命令: {},本地执行超时!'.format(cmd),"red")
                # 当shell=True时,只有os.killpg才能kill子进程
                try:
                    # time.sleep(1)
                    os.killpg(p.pid, signal.SIGUSR1)
                except Exception as e:
                    pass
                return False

    result = p.stdout.readlines()  # 结果输出列表
    return result


class NetworkTest(object):
    def __init__(self):
        self.flag_list = []

    def check_ping(self,ip,timeout):
        """
        检查ping
        :param ip: ip地址
        :param ip: 超时时间
        :return: none
        """
        cmd = "ping %s -c 2 -W %s" %(ip,timeout)
        # print(cmd)
        # 本机执行命令
        res = execute_linux2(cmd,2)
        # print("res",res,"ip",ip,"len",len(res))
        if not res:
            custom_print("错误, 执行命令: {} 失败".format(cmd), "red")
            self.flag_list.append(False)
            return False

        if len(res) != 7:
            custom_print("错误,执行命令: {} 异常".format(cmd), "red")
            self.flag_list.append(False)
            return False

        res.pop()  # 删除最后一个元素
        last_row = res.pop().decode('utf-8').strip()  # 再次获取最后一行结果
        if not last_row:
            custom_print("错误,执行命令: {} 获取结果异常","red")
            self.flag_list.append(False)
            return False

        res = last_row.split()  # 切割结果
        # print(res,type(res),len(res))
        if len(res) <10:
            custom_print("错误,切割 ping 结果异常","red")
            self.flag_list.append(False)
            return False

        if res[5] == "0%":  # 判断丢包率
            custom_print("正常, ip: {} ping正常 丢包率0%".format(ip), "green")
        else:
            self.flag_list.append(False)
            custom_print("错误, ip: {} ping异常 丢包率100%".format(ip), "red")

    def main(self):
        """
        主程序
        :return:
        """
        pool= gevent.pool.Pool(100)  # 协程池固定为100个
        ip_list = ["192.168.0.{}".format(i) for i in range(1, 256)]
        # 使用协程池,执行任务。语法: pool.map(func,iterator)
        # partial使用偏函数传递参数
        # 注意:func第一个参数,必须是迭代器遍历的值。后面的参数,必须使用有命名传参
        pool.map(partial(self.check_ping, timeout=1), ip_list)


if __name__ == '__main__':
    startime = time.time()  # 开始时间

    NetworkTest().main()

    endtime = time.time()
    take_time = endtime - startime

    if take_time < 1:  # 判断不足1秒时
        take_time = 1  # 设置为1秒
    # 计算花费时间
    m, s = divmod(take_time, 60)
    h, m = divmod(m, 60)

    custom_print("本次花费时间 %02d:%02d:%02d" % (h, m, s),"green")

执行脚本,效果同上

本文参考链接:

https://www.cnblogs.com/c-x-a/p/9049651.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
STM32F103C8T6单片机简介「建议收藏」
STM32F103C8T6是一款由意法半导体公司(ST)推出的基于Cortex-M3内核的32位微控制器,硬件采用LQFP48封装,属于ST公司微控制器中的STM32系列。除了被我们熟知的STM32,ST公司还有SPC5X系列、STM8系列等,具体参数如下:
全栈程序员站长
2022/08/10
15K0
STM32F103C8T6单片机简介「建议收藏」
STM32 最小系统
单片机最小系统是指用最少的电路组成单片机可以工作的系统,通常最小系统包含:电源电路、时钟电路、复位电路、调试/下载电路,对于STM32还需要启动选择电路。
韦东山
2022/05/05
1.7K0
STM32 最小系统
1-STM32+ESP8266+AIR202基本控制篇: 硬件使用说明
板载说明 1.主控MCU:  STM32F103C8T6 2.WIFI模块:  ESP8266-12F/E 3.GPRS模块: AIR202 (2G,移动/联通) 4.DHT11,一路继电器输出,OLED液晶 拨动开关说明
杨奉武
2020/06/28
1.5K0
如何解决STM32待机模式无法下载程序问题
STM32的待机模式(Standby Mode)是一种低功耗模式,主要用于在不需要高性能处理的情况下,降低系统的功耗。
不脱发的程序猿
2025/01/12
5100
如何解决STM32待机模式无法下载程序问题
stm32入门教程_单片机STM32
首先你得知道学习stm32,实际就是在学ARM内核,stm32内核就是ARM的; ARM使用RISC精简指令集模式开发; ARM公司全称Acorn Risc Machine; ARM处理器本身是32位设计,但也具备16位指令集,与等价32位处理器相比代码量节省35%,还能具备32位处理器的所有优势; ARM公司是英国的; ARM公司是全球知识产权提供商,他不做生产制造; 全世界超过95%的智能手机和平板电脑都采用ARM架构; 同时日本软银收购了ARM公司,成为物联网的领军者; ARM11系列就是应用到手机上的芯片,包括ARMv6、ARM6T2、ARMv6KZ、ARMv6K; ARM12系列时候,名字就不叫ARM12了,叫成Cortex; 杨桃首页:
全栈程序员站长
2022/10/29
1.8K0
stm32入门教程_单片机STM32
STM32F0单片机快速入门二 开发环境的建立 20200128
让 STM32F030 运行起来非常简单,在硬件上仅需给数字电源和模拟电源引脚(VDD,VDDA)供电,最好分别加上去耦电容。推荐值: VDD引脚加 1个4.7uF并联几个0.1uF 电容,VDDA 引脚加 1个1uF并联 1个0.01uF电容。电容值大些小些,甚至不加,对调试都不会有太大影响。
用户2366192
2021/05/31
8580
STM32介绍
什么是 STM32 STM32,从字面上来理解,ST 是意法半导体,M 是Microelectronics 的缩写,32 表示32 位,合起来理解,STM32 就是指 ST 公司开发的 32 位微控制器。STM32 是以32位为一个单位,同时能处理32位。比如51单片机就是一个同时处理8位的单片机。STM32中外设以4个位确定一个引脚功能。在如今的 32 位控制器当中,STM32 可以说是最璀璨的新星,大受工程师和市场的青睐,无芯能出其右。 CMSIS构架
跋扈洋
2021/01/29
2.6K1
STM32新手入门教程[通俗易懂]
简介参考自:小马哥STM32四轴学习平台–DragonFly四轴STM32单片机软件入门级飞控算法课程
全栈程序员站长
2022/07/25
2.6K0
STM32新手入门教程[通俗易懂]
STM32CubeMX + STM32F1系列开发时遇到的四个问题及解决方案分享
这四个问题是我在使用STM32F103C8T6 + STM32CubeMX做项目时遇到的,给大家分享一下,以下四个问题重要程度依次降低,分别是:
Mculover666
2020/07/16
3.1K1
STM32CubeMX + STM32F1系列开发时遇到的四个问题及解决方案分享
STM32F103笔记(一)[通俗易懂]
stm32F1系列是来自ARM公司具有突破性的以ARM Cortex-M3为内核的32为微处理器,内核为ARM公司为要求高性能,低功耗,低成本,性价比高的嵌入式应用专门设计的Cortex-M内核。
全栈程序员站长
2022/11/07
3.4K0
STM32F103笔记(一)[通俗易懂]
stm32开发教程_单片机STM32
有关stm32F1,stm32F4 固件驱动包的下载,请打开这篇文章: https://blog.csdn.net/xiaoeleis/article/details/105789061
全栈程序员站长
2022/10/04
9620
stm32开发教程_单片机STM32
【STM32】系统时钟RCC详解(超详细,超全面)
时钟是单片机运行的基础,时钟信号推动单片机内各个部分执行相应的指令。时钟系统就是CPU的脉搏,决定cpu速率,像人的心跳一样 只有有了心跳,人才能做其他的事情,而单片机有了时钟,才能够运行执行指令,才能够做其他的处理 (点灯,串口,ADC),时钟的重要性不言而喻。
全栈程序员站长
2022/09/14
2.8K0
【STM32】系统时钟RCC详解(超详细,超全面)
1-HC32F460(华大)+BC260Y(NB-IOT)基本控制篇(自建物联网平台)--硬件使用说明
<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/ZLIOTA_BC260Y/my.html" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>
杨奉武
2021/06/01
2.7K0
1-HC32F460(华大)+BC260Y(NB-IOT)基本控制篇(自建物联网平台)--硬件使用说明
如何使用串口来给STM32下载程序
第一次学习STM32的时候,不知道有调试器这个东西,所以一直是通过串口来给STM32下载程序,下载速度也还算可以,一般是几秒钟完成。后来用了调试器,可以直接在Keil环境下进行下载,而且还可以进行在线调试,所以后来就很少使用串口来下载程序了。前几天在uFUN试用群里看到有几个朋友在使用串口下载程序时,遇到了各种各样的问题,所以在这里简单介绍一下如何通过串口来给STM32下载程序。
单片机点灯小能手
2020/07/16
3.3K0
STM32F103C8T6芯片的重要引脚功能有哪些?
STM32F103C8T6 是 STM32F1 系列中的一款基于 ARM Cortex-M3 内核的微控制器,具有丰富的外设和引脚功能。
不脱发的程序猿
2024/11/28
1.6K0
STM32F103C8T6芯片的重要引脚功能有哪些?
聊一聊STM32的低功耗管理(附源码)
先来看看STM系列手册为例看看STM32的几种工作模式,小飞哥最近用到STM32G0系列的MCU,就拿G0的手册来聊一聊吧,其他的都类似,功耗方面有些差别
用户8913398
2022/06/07
2.2K0
聊一聊STM32的低功耗管理(附源码)
STM32学习笔记(超详细整理145个问题)
1、AHB系统总线分为APB1(36MHz)和APB2(72MHz),其中2>1,意思是APB2接高速设备;
全栈程序员站长
2022/08/18
1.6K0
STM32学习笔记(超详细整理145个问题)
【STM32】GPIO口以及EXTI外部中断
输出模式下可控制端口输出高低电平,用以驱动LED、控制蜂鸣器、模拟通信协议输出时序等
s-little-monster
2024/08/09
7280
【STM32】GPIO口以及EXTI外部中断
stm32如何才能正常运行的调试笔记
想使用野火或者安福来的代码模板来学习这个stm32,毕竟他买使用量挺多的,代码风格尤其是安福来比较好,因此想试试他们的工程,但是弄了好长一段时间,单片机就是不能运行,进入debug,就死机,停在硬件错误或其他地方。
用户4645519
2020/09/07
5220
stm32 boot0硬件接法导致的概率性启动失败问题总结和反思
问题概要,板子在稳压电源上工作很好,可一旦接了电池,stm32就会出现概率性的无法启动。加上项目比较急,这个问题阻塞一直无法量产。真是非常的要命啊。
番茄老夫子
2023/11/03
1K0
推荐阅读
相关推荐
STM32F103C8T6单片机简介「建议收藏」
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档