Python threading多线程编程实例
admin
2023-07-31 02:12:59
0

Python 的多线程有两种实现方法:

函数,线程类

1.函数

调用 thread 模块中的 start_new_thread() 函数来创建线程,以线程函数的形式告诉线程该做什么

复制代码 代码如下:
# -*- coding: utf-8 -*-
import thread
def f(name):
  #定义线程函数
  print \”this is \” + name
 
if __name__ == \’__main__\’:
  thread.start_new_thread(f, (\”thread1\”,))
  #用start_new_thread()调用线程函数和其他参数
  while 1:
    pass

不过这种方法暂时没能找到其他辅助方法,连主线程等待都要用 while 1 这种方法解决。

2.线程类

调用 threading 模块,创建 threading.Thread 的子类来得到自定义线程类。

复制代码 代码如下:
# -*- coding: utf-8 -*-
import threading
class Th(threading.Thread):
  def __init__(self, name):
    threading.Thread.__init__(self)
    self.t_name = name
    #调用父类构造函数
 
  def run(self):
    #重写run()函数,线程默认从此函数开始执行
    print \”This is \” + self.t_name
 
if __name__ == \’__main__\’:
  thread1 = Th(\”Thread_1\”)
  thread1.start()
  #start()函数启动线程,自动执行run()函数

threading.Thread 类的可继承函数:
getName() 获得线程对象名称
setName() 设置线程对象名称
join() 等待调用的线程结束后再运行之后的命令
setDaemon(bool) 阻塞模式, True: 父线程不等待子线程结束, False 等待,默认为 False
isDaemon() 判断子线程是否和父线程一起结束,即 setDaemon() 设置的值
isAlive() 判断线程是否在运行

实例

复制代码 代码如下:
import threading
import time
class Th(threading.Thread):
  def __init__(self, thread_name):
    threading.Thread.__init__(self)
    self.setName(thread_name)
 
  def run(self):
    print \”This is thread \” + self.getName()
    for i in range(5):
      time.sleep(1)
      print str(i)
    print self.getName() + \”is over\”

join() 阻塞等待

复制代码 代码如下:
if __name__ == \’__main__\’:
    thread1 = Th(\”T1 \”)
    thread1.start()
    #thread1.join()
    print \”main thread is over\”

不带 thread1.join() ,得到如下结果:

复制代码 代码如下:
This is thread T1
main thread is over
0
1
2
T1 is over

不等待 thread1 完成,执行之后语句。
加了 thread1.join() ,得到如下结果:
复制代码 代码如下:
This is thread T1
0
1
2
T1 is over
main thread is over

阻塞等待 thread1 结束,才执行下面语句

主线程等待

复制代码 代码如下:
if __name__ == \’__main__\’:
  thread1 = Th(\”T1 \”)
  thread1.setDaemon(True)
  #要在线程执行之前就设置这个量
  thread1.start()
  print \”main thread is over\”

报错: Exception in thread T1 (most likely raised during interpreter shutdown):
也就是主线程不等待子线程就结束了。

多个子线程

复制代码 代码如下:
if __name__ == \’__main__\’:
    for i in range(3):
        t = Th(str(i))
        t.start()
    print \”main thread is over\”

这里的 t 可同时处理多个线程,即 t 为线程句柄,重新赋值不影响线程。

这里奇怪的是,运行 t.run() 时,不会再执行其他线程。虽不明,还是用 start() 吧。暂且理解为 start() 是非阻塞并行的,而 run 是阻塞的。

线程锁

threading 提供线程锁,可以实现线程同步。

复制代码 代码如下:
import threading
import time
class Th(threading.Thread):
  def __init__(self, thread_name):
    threading.Thread.__init__(self)
    self.setName(thread_name)
 
  def run(self):
    threadLock.acquire()
    #获得锁之后再运行
    print \”This is thread \” + self.getName()
    for i in range(3):
      time.sleep(1)
      print str(i)
    print self.getName() + \” is over\”
    threadLock.release()
    #释放锁
if __name__ == \’__main__\’:
  threadLock = threading.Lock()
  #设置全局锁
  thread1 = Th(\’Thread_1\’)
  thread2 = Th(\’Thread_2\’)
  thread1.start()
  thread2.start()

得到结果:

复制代码 代码如下:
This is thread Thread_1
0
1
2
Thread_1 is over
This is thread Thread_2
0
1
2
Thread_2 is over

相关内容

python使用第三方库 ...
要在 Python 中合并 PDF 文件,你可以使用第三方库 Py...
2024-04-08 00:19:36
用python统计文件夹下...
要统计文件夹A下每个子文件夹的大小,你可以使用递归方法来实现。具体...
2024-03-12 02:14:05
使用Python的多线程模...
在默认情况下,SCP命令本身并不支持多线程。SCP是通过SSH协议...
2024-03-03 03:41:49
pycparser 是一...
`pycparser` 是一个用 Python 编写的 C 语言解...
2024-02-15 00:57:45
pycparser 是一...
pycparser 是一个用于解析 C 语言代码的 Python ...
2024-02-05 21:23:45
在Python中,使用Py...
在Python中,你可以使用一些库来提取PDF文档中的文字。一个常...
2024-02-05 21:20:00

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
Apache Doris 常见... 什么是 Apache Doris Apache Doris 是一款 MPP 架构的 OLAP 列式存...
Vmware简易安装ubunt... 大晚上的折腾死我了VMware安装ubuntu,用简易安装结果设置的用户名密码死活进不去再重装一次,...
‘WebDriver‘ obj... selenium库报错"‘WebDriver’ object has no attribute ‘f...
WiFi中继器和WiFi扩展器... WiFi中继器以无线方式连接到 WiFi 网络并重新广播信号。它就像一个中继系统,连接到我们的 Wi...
mysql插入数据到数据库时失... 插入数据到数据库时失败:Timeout in IO operation 查看mysql日志 显示是磁...