Python中的线程threading.Thread()使用详解

进行程序开发的时候,肯定避免不了要处理并发的情况,一般使用多线程实现并发
python实现多线程编程需要借助于threading模块。

Thread

threading 模块中最核心的内容是 Thread 这个类。
程序运行时默认就是在主线程上,创建 Thread 对象,然后让它们运行,每个 Thread 对象代表一个线程,在每个线程中我们可以让程序处理不同的任务,这就是多线程编程。

创建Thread对象

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

  • target: 传一个回调函数对象
  • 如果回调函数对象需要传递参数的话,args 是固定参数,kwargs 是可变参数
import threading
import time

def test():
    for i in range(5):
        print('test ',i)
        time.sleep(1)
        
thread = threading.Thread(target=test)
启动Thread 对象
thread.start()
主线程加一个线程整体代码运行
import threading
import time

def test():
    for i in range(5):
        print('test ',i)
        time.sleep(1)
        
thread = threading.Thread(target=test)
thread.start()

for i in range(5):
    print('main ', i)
    time.sleep(1)

上面代码很简单,在主线程上打印 5 次,在一个子线程上打印 5 次。

运行结果如下:

在这里插入图片描述

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用提供了关于使用threading.Thread创建和管理线程的示例代码。在该示例threading.Thread对象需要传入一个函数名和参数表作为参数,并且可以通过start方法启动线程,通过join方法等待线程的结束。此外,threading.Thread对象还可以自动地帮助我们管理线程锁。 引用提供了另一个关于使用threading.Thread的示例代码。在该示例,定义了一个worker函数作为线程的目标函数,并使用threading.Thread对象创建了一个名为'worker'的线程。通过调用start方法,线程开始执行worker函数的任务。 根据这两个引用的内容,我们可以看到,threading.Thread对象是用于创建和管理线程的一个重要工具。它可以帮助我们在Python实现并发执行的功能,并且可以通过传入不同的目标函数和参数来执行不同的任务。 然而,threading.Thread对象本身并不直接与Linux命令相关联。如果您希望在Python运行Linux命令,可以使用subprocess模块来实现。subprocess模块提供了一个创建子进程的接口,可以在子进程执行系统命令。您可以使用subprocess模块的Popen函数来创建一个子进程对象,并使用该对象的communicate方法来与子进程进行交互并获取命令的输出结果。 以下是一个示例代码,演示了如何使用subprocess模块在Python运行Linux命令: ```python import subprocess # 定义要执行的Linux命令 command = "ls -l" # 创建子进程对象 process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 获取命令的输出结果 output, error = process.communicate() # 打印输出结果 print(output.decode()) ``` 在这个示例,我们使用subprocess.Popen函数创建了一个子进程对象,并传入要执行的Linux命令作为参数。然后,使用communicate方法与子进程进行交互,并获取命令的输出结果。最后,使用print函数打印输出结果。 总结起来,threading.Thread对象的主要作用是创建和管理线程,而与Linux命令的执行无直接关系。要在Python执行Linux命令,可以使用subprocess模块。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Python学习笔记】(八)多线程与并行:_thread模块、threading模块、Queue模块;os模块、subprocess模块...](https://blog.csdn.net/weixin_43931465/article/details/106932333)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python 进程和线程详解(daemon/non-daemon/join/threading.local)](https://blog.csdn.net/weixin_44983653/article/details/124288141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值