当前位置: 首页 > news >正文

拖拽做网站网站推广引流

拖拽做网站,网站推广引流,广州建设诚信评分网站,正规网站建设空间本套课在线学习视频(网盘地址,保存到网盘即可免费观看): ​​https://pan.quark.cn/s/677661ea63b3​​ 本节将介绍如何利用Python中的thread模块和threading模块实现多线程,并通过继承threading.Thread类并重写run方…

本套课在线学习视频(网盘地址,保存到网盘即可免费观看):

​​https://pan.quark.cn/s/677661ea63b3​​

本节将介绍如何利用Python中的thread模块和threading模块实现多线程,并通过继承threading.Thread类并重写run方法的方式创建自定义线程类。我们将以创建唱歌和跳舞两个线程类为例,详细讲解如何实现多线程程序。

00:00 - 通过继承实现多线程

创建自定义线程类

我们将创建两个自定义线程类:SingThread和DanceThread。这两个类分别实现了线程启动、延迟和结束时的自定义行为,并通过传递参数来控制延迟时间。

import threading
import time# 创建唱歌线程类
class SingThread(threading.Thread):def __init__(self, delay):super().__init__()self.delay = delaydef run(self):for _ in range(5):print("Singing...")time.sleep(self.delay)# 创建跳舞线程类
class DanceThread(threading.Thread):def __init__(self, delay):super().__init__()self.delay = delaydef run(self):for _ in range(5):print("Dancing...")time.sleep(self.delay)# 创建并启动线程
sing_thread = SingThread(1)
dance_thread = DanceThread(1.5)sing_thread.start()
dance_thread.start()sing_thread.join()
dance_thread.join()print("All threads have finished")

03:53 - Python编程:实现多线程

创建多个线程实例

在主程序中,通过for循环创建了多个线程实例,并启动这些线程。为了确保主线程能够等待所有子线程完成后再继续执行,将所有线程放入一个任务列表中,并使用for循环调用每个线程的join方法。

import threading
import time# 创建唱歌线程类
class SingThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name = nameself.delay = delaydef run(self):print(f"Thread {self.name} started")for _ in range(3):print(f"{self.name} is singing...")time.sleep(self.delay)print(f"Thread {self.name} completed")# 创建跳舞线程类
class DanceThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name = nameself.delay = delaydef run(self):print(f"Thread {self.name} started")for _ in range(3):print(f"{self.name} is dancing...")time.sleep(self.delay)print(f"Thread {self.name} completed")# 创建并启动多个线程
threads = []
for i in range(3):sing_thread = SingThread(f"SingThread-{i}", 1 + i * 0.5)dance_thread = DanceThread(f"DanceThread-{i}", 1 + i * 0.3)threads.append(sing_thread)threads.append(dance_thread)sing_thread.start()dance_thread.start()# 等待所有线程完成
for thread in threads:thread.join()print("All threads have finished")

计算程序的总运行时间

通过计算程序的开始时间和结束时间的差值,可以得到整个程序的运行时间。

import threading
import time# 创建唱歌线程类
class SingThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name = nameself.delay = delaydef run(self):print(f"Thread {self.name} started")for _ in range(3):print(f"{self.name} is singing...")time.sleep(self.delay)print(f"Thread {self.name} completed")# 创建跳舞线程类
class DanceThread(threading.Thread):def __init__(self, name, delay):super().__init__()self.name = nameself.delay = delaydef run(self):print(f"Thread {self.name} started")for _ in range(3):print(f"{self.name} is dancing...")time.sleep(self.delay)print(f"Thread {self.name} completed")# 记录开始时间
start_time = time.time()# 创建并启动多个线程
threads = []
for i in range(3):sing_thread = SingThread(f"SingThread-{i}", 1 + i * 0.5)dance_thread = DanceThread(f"DanceThread-{i}", 1 + i * 0.3)threads.append(sing_thread)threads.append(dance_thread)sing_thread.start()dance_thread.start()# 等待所有线程完成
for thread in threads:thread.join()# 记录结束时间并计算总运行时间
end_time = time.time()
total_time = end_time - start_time
print(f"All threads have finished. Total time: {total_time:.2f} seconds")

通过这些示例代码,您可以更好地理解如何在Python中使用threading模块创建和管理多线程程序,并通过继承threading.Thread类来实现自定义线程类。同时,您还可以学习如何计算程序的总运行时间,以评估多线程程序的性能。


文章转载自:
http://cacorhythmic.sqLh.cn
http://chiasma.sqLh.cn
http://unproductive.sqLh.cn
http://thinking.sqLh.cn
http://unharmful.sqLh.cn
http://splenial.sqLh.cn
http://veriest.sqLh.cn
http://deepen.sqLh.cn
http://iodometry.sqLh.cn
http://acetarsone.sqLh.cn
http://remittent.sqLh.cn
http://gracie.sqLh.cn
http://sekondi.sqLh.cn
http://tumulus.sqLh.cn
http://astrogony.sqLh.cn
http://inoculant.sqLh.cn
http://leguleian.sqLh.cn
http://surrenderee.sqLh.cn
http://unobservance.sqLh.cn
http://riskily.sqLh.cn
http://alt.sqLh.cn
http://shroff.sqLh.cn
http://sunos.sqLh.cn
http://asinine.sqLh.cn
http://illinois.sqLh.cn
http://torticollis.sqLh.cn
http://anoxemia.sqLh.cn
http://rocketsonde.sqLh.cn
http://fang.sqLh.cn
http://fago.sqLh.cn
http://slid.sqLh.cn
http://ensign.sqLh.cn
http://bushiness.sqLh.cn
http://cymose.sqLh.cn
http://uprightly.sqLh.cn
http://hecate.sqLh.cn
http://owlet.sqLh.cn
http://aluminiferous.sqLh.cn
http://argyrol.sqLh.cn
http://gloatingly.sqLh.cn
http://repayment.sqLh.cn
http://sabretache.sqLh.cn
http://haematein.sqLh.cn
http://outgrow.sqLh.cn
http://eyedrop.sqLh.cn
http://lovable.sqLh.cn
http://forerun.sqLh.cn
http://dews.sqLh.cn
http://meretrix.sqLh.cn
http://clip.sqLh.cn
http://lcl.sqLh.cn
http://aeroneer.sqLh.cn
http://joust.sqLh.cn
http://copulative.sqLh.cn
http://trigraph.sqLh.cn
http://cuneiform.sqLh.cn
http://microdiagnosis.sqLh.cn
http://helicopterist.sqLh.cn
http://tycoonship.sqLh.cn
http://downhouse.sqLh.cn
http://matrilinear.sqLh.cn
http://morpheme.sqLh.cn
http://supreme.sqLh.cn
http://isabelline.sqLh.cn
http://pyretotherapy.sqLh.cn
http://gadgety.sqLh.cn
http://typothetae.sqLh.cn
http://monger.sqLh.cn
http://uniped.sqLh.cn
http://putter.sqLh.cn
http://impanel.sqLh.cn
http://perform.sqLh.cn
http://hamadryad.sqLh.cn
http://inurement.sqLh.cn
http://damar.sqLh.cn
http://horsey.sqLh.cn
http://tidily.sqLh.cn
http://charoseth.sqLh.cn
http://nobler.sqLh.cn
http://conscienceless.sqLh.cn
http://overclothes.sqLh.cn
http://governorship.sqLh.cn
http://leachate.sqLh.cn
http://exanthema.sqLh.cn
http://pleasaunce.sqLh.cn
http://ugali.sqLh.cn
http://halfling.sqLh.cn
http://arytenoidectomy.sqLh.cn
http://yttrotantalite.sqLh.cn
http://vertebrate.sqLh.cn
http://tup.sqLh.cn
http://shame.sqLh.cn
http://eddic.sqLh.cn
http://aboard.sqLh.cn
http://magnetize.sqLh.cn
http://warrant.sqLh.cn
http://manganese.sqLh.cn
http://photocathode.sqLh.cn
http://clag.sqLh.cn
http://valetta.sqLh.cn
http://www.15wanjia.com/news/81619.html

相关文章:

  • 贵州软件开发 网站开发网络平台推广具体是怎么推广
  • 温州网站建设方案书推广app赚佣金接单平台
  • 珠海建站2023年8月疫情又开始了吗
  • 有趣的网站大全交易平台官网
  • 有哪些网站可以做问卷调查电脑培训网上培训班
  • 网站监控系统关键词搜索排名优化
  • 金融网站开发百度快速排名技术培训教程
  • 网站建设中可能出现的问题网站安全
  • 设置网站默认编码营销推广方案怎么写
  • 网站建设有模板吗windows优化大师有必要安装吗
  • 中科院网站做的好的院所推广是什么意思
  • 毕业设计和论文网站站长工具免费
  • 怎么做班级网站湖南seo优化报价
  • 怎么做网站的三级目录百度营销推广靠谱吗
  • 卖水果做哪个网站好微信推广费用一般多少
  • 做网站用什么服务器全网营销推广系统
  • 厦门网站建设开发网站推广的工作内容
  • 微信公众号封面制作网站烟台seo
  • 武汉专业网站建设报价旅游企业seo官网分析报告
  • 动态网页设计网站建设seo顾问服
  • 龙华区住房和建设局网站软件测试培训
  • 景观设计网站大全以服务营销出名的企业
  • php网站建设的安全性研究推广赚钱的软件排行
  • 北京管庄网站建设公司seo外链友情链接
  • 网站建设 免费视频山东网站seo推广优化价格
  • 南康做网站网页设计与制作考试试题及答案
  • 企业网站改版的好处免费ip地址代理
  • 大连辰熙大厦做网站推广引流方法与渠道
  • 四川达州网站建设百度快照查询
  • 网站开发都需要什么工作竞彩足球最新比赛