python如何开启多线程

 更新时间:2023年08月14日 08:39:17   作者:Audreybiubiu  
这篇文章主要介绍了python如何开启多线程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

python开启多线程

为了加快程序运行速度,对相同功能的一些执行语句,python可以通过 ThreadPool 做到

重要的函数为

pool = ThreadPool(processes=3)
pool.apply_async(func, args=(**krags))
pool.close()
pool.join()
from multiprocessing.pool import ThreadPool
def parallel(self, cls, driven_data_key=None):
    if not self.FINAL_TEMPLATE:
        self.get_final_templates()
    # 开启线程池里线程的数量
    pool = ThreadPool(processes=len(self.FINAL_TEMPLATE))
    # 当前的for循环实则并行执行
    for top_template in self.FINAL_TEMPLATE:
        # 第一个参数为想要并行执行的函数,第二个参数为要执行的函数所需要的参数
        pool.apply_async(self.filter_case_online, args=(cls, top_template))
    pool.close()
    pool.join()

python开启多线程/停止多线程

import ctypes
import inspect
import threading
import time
def main(a):
    while True:
        print(a)
class myThread(threading.Thread):  # 继承父类threading.Thread
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name
    def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
        main(self.name)
def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)
if __name__ == '__main__':
    nameList = [1, 2, 3, 4, 5, 6]
    threadList = []
    for name in nameList:
        threadList.append(myThread(str(name)))
    # 开启线程
    for thread in threadList:
        thread.start()
    # 停止线程
    time.sleep(1)
    for thread in threadList:
        stop_thread(thread)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解如何使用Python网络爬虫获取招聘信息

    详解如何使用Python网络爬虫获取招聘信息

    在疫情阶段,想找一份不错的工作变得更为困难,很多人会选择去网上看招聘信息。可是招聘信息有一些是错综复杂的。本文将为大家介绍用Python爬虫获取招聘信息的方法,需要的可以参考一下
    2022-03-03
  • Python基于callable函数检测对象是否可被调用

    Python基于callable函数检测对象是否可被调用

    这篇文章主要介绍了Python基于callable函数检测对象是否可被调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • 详解Python中matplotlib模块的绘图方式

    详解Python中matplotlib模块的绘图方式

    Matplotlib是Python中最受欢迎的数据可视化软件包之一,它是 Python常用的2D绘图库,同时它也提供了一部分3D绘图接口。本文将详细介绍Matplotlib的绘图方式,需要的可以参考一下
    2022-07-07
  • Python中使用 Selenium 实现网页截图实例

    Python中使用 Selenium 实现网页截图实例

    这篇文章主要介绍了Python中使用 Selenium 实现网页截图实例,Selenium支持Java、C#、Ruby 以及 Python等语言,本文以Python语言为例,需要的朋友可以参考下
    2014-07-07
  • python 批量修改 labelImg 生成的xml文件的方法

    python 批量修改 labelImg 生成的xml文件的方法

    这篇文章主要介绍了python 批量修改 labelImg 生成的xml文件的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Python中six模块基础用法

    Python中six模块基础用法

    在本篇文章里小编给大家分享的是关于Python中six模块基础用法以及相关知识点,需要的朋友们学习下。
    2019-12-12
  • Django数据库操作之save与update的使用

    Django数据库操作之save与update的使用

    这篇文章主要介绍了Django数据库操作之save与update的使用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Jupyter Notebook 实现正常显示中文和负号

    Jupyter Notebook 实现正常显示中文和负号

    这篇文章主要介绍了Jupyter Notebook 实现正常显示中文和负号,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • python实现ping的方法

    python实现ping的方法

    这篇文章主要介绍了python实现ping的方法,以实例形式较为详细的分析了Python发送ICMP数据包实现ping功能的相关技巧,需要的朋友可以参考下
    2015-07-07
  • python实现数通设备端口监控示例

    python实现数通设备端口监控示例

    这篇文章主要介绍了python实现数通设备端口监控示例,需要的朋友可以参考下
    2014-04-04

最新评论