vs做网站怎样添加图片,wordpress 转盘插件,live2d看板娘wordpress,seo外链优化#x1f308;个人主页: 程序员不想敲代码啊 #x1f3c6;CSDN优质创作者#xff0c;CSDN实力新星#xff0c;CSDN博客专家 #x1f44d;点赞⭐评论⭐收藏 #x1f91d;希望本文对您有所裨益#xff0c;如有不足之处#xff0c;欢迎在评论区提出指正#xff0c;让我们共… 个人主页: 程序员不想敲代码啊 CSDN优质创作者CSDN实力新星CSDN博客专家 点赞⭐评论⭐收藏 希望本文对您有所裨益如有不足之处欢迎在评论区提出指正让我们共同学习、交流进步 Python多线程 1. 前言2. threading 模块的基本用法3. Thread类4. 锁Locks5. 守护线程Daemon Threads6. 运用场景7. 弊端 1. 前言
Python中的多线程通过threading模块来实现它允许你并发执行多个线程线程是操作系统能够独立调度的最小单位它通常被用来执行并行任务。
在解释Python的多线程之前需要注意的是由于全局解释器锁Global Interpreter LockGIL的存在CPythonPython的主要实现版本中的多线程通常不能在多个CPU核心中并行执行GIL确保一次只有一个线程在Python对象上执行操作防止并发访问导致的状态不一致。因此Python的多线程更适用于I/O密集型任务而非CPU密集型任务。
2. threading 模块的基本用法
以下是使用threading模块创建和启动一个线程的基本例子
import threading
import time# 定义一个函数用于线程执行
def my_function(arg1, arg2):for i in range(arg1, arg2):print(fThread running: {i})time.sleep(1) # 模拟耗时操作# 创建线程
thread threading.Thread(targetmy_function, args(1, 10))# 启动线程
thread.start()# 在主线程中继续执行其他操作
for i in range(20, 25):print(fMain thread running: {i})time.sleep(1)# 等待直到线程完成
thread.join()3. Thread类
在threading模块里Thread是一个代表线程的类你可以创建一个Thread类的实例并调用它的start()方法来运行新线程。每个Thread可以运行一个函数或方法。
除了直接使用threading.Thread你也可以通过继承Thread类来定义新的线程子类重写其中的run()方法
class MyThread(threading.Thread):def __init__(self, arg1, arg2):super().__init__()self.arg1 arg1self.arg2 arg2def run(self):for i in range(self.arg1, self.arg2):print(fRunning from the extended thread class: {i})time.sleep(1)# 使用自定义线程类
thread MyThread(1, 5)
thread.start()
thread.join()4. 锁Locks
锁是一个同步原语用于防止多个线程同时访问共享资源在Python中你可以使用threading.Lock()来创建一个锁。锁有两个基本方法acquire()和release()。当一个线程通过调用acquire()获得了锁它会阻止其他线程获取直到它调用release()释放锁。
# 创建一个锁
lock threading.Lock()# 在需要访问共享资源前获取锁
lock.acquire()# 访问共享资源
# ...# 完成共享资源的访问后释放锁
lock.release()可以使用 with 语句简化以上模式这样可以保证锁被正确释放
with lock:# 访问共享资源# ...5. 守护线程Daemon Threads
守护线程是一种特殊的线程它在主线程退出时也会随之退出与常规线程相比守护线程不用等待它完成才能退出程序。通过设置线程的daemon属性为True可以将线程声明为守护线程
thread threading.Thread(targetmy_function, args(1, 10))
thread.daemon True
thread.start()6. 运用场景
在Python中多线程经常被用于处理I/O密集型任务例如文件读写、网络通讯等等。对于CPU密集型任务多进程通常是更好的选择。
7. 弊端
由于GIL的存在在CPython的多线程程序不会真正地并行执行多线程即使在多核CPU上。为了克服这个限制可以考虑使用multiprocessing模块这个模块通过创建多个进程来实现真正的并行计算每个进程有自己的GIL。当然每个场景下都需要仔细考虑是否适合多线程、多进程或者其他解决方案比如异步编程asyncio或者其他并发框架如concurrent.futures。 文章转载自: http://www.morning.rbqlw.cn.gov.cn.rbqlw.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.knpmj.cn.gov.cn.knpmj.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.xrqkm.cn.gov.cn.xrqkm.cn http://www.morning.gjzwj.cn.gov.cn.gjzwj.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.dmwck.cn.gov.cn.dmwck.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.brhxd.cn.gov.cn.brhxd.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.ndngj.cn.gov.cn.ndngj.cn http://www.morning.plfy.cn.gov.cn.plfy.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.ypfw.cn.gov.cn.ypfw.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.npfkw.cn.gov.cn.npfkw.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn