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

电子商务网站建设asp谷歌google官方下载

电子商务网站建设asp,谷歌google官方下载,页面布局怎么设置,闲鱼网站如何赚钱并发编程-线程 一个进程是操作系统中运行的一个任务,进程独立拥有CPU、内存等资源一个线程是一个进程中运行的一个任务,线程之间共享CPU、内存等资源,进程里的每一个任务都是线程。 线程创建 创建线程:使用threading模块中的Th…

并发编程-线程

  • 一个进程是操作系统中运行的一个任务,进程独立拥有CPU、内存等资源
  • 一个线程是一个进程中运行的一个任务,线程之间共享CPU、内存等资源,进程里的每一个任务都是线程。

线程创建

创建线程:使用threading模块中的Thread类来创建线程

  • target表示线程执行的任务
  • args表示任务的参数,是一个元组
  • start()方法指启动线程
  • join()方法指等待线程结束
from threading import Threaddef task(count:int):...thread1 = Thread(target=task, args=(10,))  # 创建子线程thread1
thread1.start()  # 启动子线程

启动两个线程执行task任务

from threading import Threaddef task(count: int):for i in range(count):print(i)thread1 = Thread(target=task, args=(10,))
thread2 = Thread(target=task, args=(20,))thread1.start()  # 启动线程1
thread2.start()	 # 启动线程2
print("main thread is end")  # 执行到此处,主线程无任务,自行结束

输出结果:

0
1
2
3
4
5
06main thread is end78
9
1
2
3
4
5
6
7
8
9

等待主线程结束:

from threading import Threaddef task():for i in range(10):print(i)thread1 = Thread(target=task)
thread2 = Thread(target=task)thread1.start()
thread2.start()
thread1.join()  # 等待线程执行结束
thread2.join()  # 等待线程执行结束
print("main thread is end")  # 等到线程1和线程2任务执行完毕,主线程才会结束

输出结果:

0
1
2
3
4
50
6
7
8
9
12
3
4
5
6
7
8
9
main thread is end

通过继承创建线程

通过继承Thread类来创建线程

打印混乱的原因:多线程执行时,print函数打印内容后会再打印换行,导致换行来不及打印,出现打印混乱

解决办法:行尾手动加换行,print函数end参数设置为空字符串

print(f"{self.name} - {i}\n", end="")
from threading import Threadclass MyThread(Thread):def run(self) -> None:# 线程的任务脚本pass
import time
from threading import Threadclass MyThread(Thread):def __init__(self, name: str, count: int):super().__init__()# 该方法已弃用,使用name属性替换# self.setName()self.name = name  # 设置线程名称self.count = countdef run(self) -> None:# 线程的任务脚本for i in range(self.count):print(f"{self.name} - {i}\n", end="")# 休眠10毫秒time.sleep(0.01)t1 = MyThread("a", 10)
t2 = MyThread("b", 10)
t1.start()
t2.start()

输出结果:

D:\Python3.11\python.exe F:/Code/python_code/high_python/python_advance/thread/demo01_class.py
a - 0
b - 0
a - 1
b - 1
b - 2
a - 2
a - 3
b - 3
b - 4
a - 4
b - 5
a - 5
b - 6
a - 6
b - 7
a - 7
b - 8
a - 8
b - 9
a - 9

守护线程

主线程结束,守护线程会自动结束,这就叫守护线程

  • 守护线程会在主线程结束时候自动结束
  • 主线程则需要等待到所有的非守护线程结束才能结束
  • 守护线程一般用于非关键性的线程,比如:日志
from threading import Threaddef task():for i in range(10):print(i)thread = Thread(target=task, daemon=True)
thread.start()

输出结果:

0
Process finished with exit code 0

等待线程结束

from threading import Threaddef task():for i in range(10):print(i)thread = Thread(target=task, daemon=True)
thread.start()
thread.join()

输出结果:

D:\Python3.11\python.exe F:/Code/python_code/high_python/python_advance/thread/demo03_daemon_thread.py
0
1
2
3
4
5
6
7
8
9Process finished with exit code 0

继承类设置守护线程

class MyThread(Thread):def __init__(self, name: str, count: int):super().__init__()# 该方法已弃用,使用name属性替换# self.setName()self.name = name  # 设置线程名称self.count = countself.daemon = Truedef run(self) -> None:# 线程的任务脚本for i in range(self.count):print(f"{self.name} - {i}\n", end="")# 休眠10毫秒time.sleep(0.01)t1 = MyThread("a", 10)
t2 = MyThread("b", 10)
t1.start()
t2.start()
t1.join() # 等待t1结束,结束主线程

线程安全队列

queue模块中的Queue类提供了线程安全队列功能

  • put(item, block=False) # 队列元素满时,不阻塞,会抛出异常,为True时,会让线程阻塞等待, 可能会导致线程卡死
  • put(item, timeout=3) # 队列元素满时,等待timeout时间,如果超出时间则抛出异常
  • get(block=False) # 从队列取元素,如果队列为空则不阻塞,会抛出异常
  • get(timeout=10) # 从队列取元素,如果队列为空则则赛等待10秒,超时抛出异常
  • qsize() # 队列大小
  • empty() # 判断队列是否为空
  • full() # 判断队列是否满的

生产者消费者线程实例

from threading import Thread
from queue import Queueclass Producer(Thread):"""生产者"""def __init__(self, name: str, count: int, queue: Queue) -> None:super(Producer, self).__init__()# 线程自带属性self.name = name# 自定义属性self.count = countself.queue = queuedef run(self) -> None:for n in range(self.count):msg = f"{self.name} - {n}"self.queue.put(msg, block=True)class Consumer(Thread):"""消费者"""def __init__(self, name: str, queue: Queue) -> None:super().__init__()self.name = nameself.daemon = Trueself.queue = queuedef run(self) -> None:while True:msg = self.queue.get(block=True)print(f"{self.name} - {msg}\n", end="")queue = Queue(maxsize=3)
threads = [Producer("p1", 10, queue),Producer("p2", 10, queue),Producer("p3", 10, queue),Consumer("c1", queue),Consumer("c2", queue),
]for t in threads:t.start()

线程池

  • 线程的创建和销毁相对比较耗费资源
  • 频繁的创建和销毁线程不利于高性能
  • 线程池是python提供的便于线程管理和提高性能的工具

python提供两个类来管理线程

  1. concurrent.futures.ThreadPoolExecutor
    • submit() # 启动/执行一个任务,返回结果是一个Future对象
    • map() # 多个任务执行,将不同参数分配到每一个任务中
    • shutdown() # 关闭线程池
  2. Future
    • result() # 任务执行结果
    • exception() # 任务异常信息

方式一:适用于不同任务

from concurrent.futures import ThreadPoolExecutor
import timedef task(name: str):print(f"{name} - step 1\n", end="")time.sleep(1)print(f"{name} - step 2\n", end="")return f"{name} complete"with ThreadPoolExecutor() as executor:result_1 = executor.submit(task, "A")result_2 = executor.submit(task, "B")print(result_1.result())  # result()会等待有结果再返回print(result_2.result())"""
A - step 1
B - step 1
A - step 2
A complete
B - step 2
B complete
"""

方式二:map()适用于同一任务,不同参数

from concurrent.futures import ThreadPoolExecutor
import timedef task(name: str) -> str:print(f"{name} - step 1\n", end="")time.sleep(1)print(f"{name} - step 2\n", end="")return f"{name} complete"with ThreadPoolExecutor() as executor:results = executor.map(task, ["C", "D"])for r in results:print(r)"""
C - step 1
D - step 1
D - step 2
C - step 2
C complete
D complete
"""

多线程案例:下载图片

from concurrent.futures import ThreadPoolExecutor
from urllib.request import urlopen, Request
import osdef download_img(url: str):site_url = Request(url, headers={})with urlopen(site_url) as web_file:img_data = web_file.read()if not img_data:raise Exception(f"Error: can not load the image from {url}")file_name = os.path.basename(url)with open(file_name, "wb") as f:f.write(img_data)return "Download image successfully, {}".format(url)urls = ["https://img0.bdstatic.com/static/searchresult/img/baseimg3_4f26a23.png",# "..."
]
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36","content-type": "Content-Type: image/jpeg"
}
with ThreadPoolExecutor() as ececutor:results = ececutor.map(download_img, urls)for r in results:print(r)

执行结果:

PS F:\Code\python_code\high_python\python_advance\thread> python .\thread_pool_demo.py
Download image successfully, https://img0.bdstatic.com/static/searchresult/img/baseimg3_4f26a23.png
http://www.qdjiajiao.com/news/2892.html

相关文章:

  • ppt免费网站产品市场推广方案
  • 南宁网站建设网站网站怎么优化
  • 帝国转wordpress程序重庆seo推广运营
  • 如何编辑网站标题栏在线排名优化工具
  • java做的网站用什么发布手机百度如何发布作品
  • 网站建设方案下载怎么免费制作网页
  • 阿里万网域名注册官网广州:推动优化防控措施落
  • 深圳龙华网站公司网站优化推广的方法
  • 2022恢复线下教学通知百度关键词seo推广
  • 建设网站需要多少时间海口seo计费
  • 甘肃高端建设网站互联网项目推广是什么
  • 网站建设xs029汕头网站建设公司哪个好
  • 怎么做二级网站河北百度推广电话
  • 网站建设开发案例seo工作内容和薪资
  • 什么网站建设策划方案 论文新的seo网站优化排名 网站
  • wordpress修改数据库配置文件seo流量是什么
  • 外贸b2b平台有哪些平台百度seo在哪里
  • 合肥网站优化费用山西seo排名
  • 口腔医院网站开发产品推广语
  • 微网站建设及微信推广方案企业网站seo诊断报告
  • 如何做网站卡密广西seo公司
  • 西安房产网站制作公司新浪微指数
  • 山东省建设教育集团网站首页西安外包网络推广
  • 局域网聊天工具有哪些上海谷歌seo推广公司
  • wordpress 侧边栏调整南宁网站优化
  • 沈阳有什么网站微信公众号推广网站
  • 免费自建网站步骤2023年8月新冠疫情
  • 购物小程序开发公司seo薪酬如何
  • 产品设计论文网站seo公司哪家好
  • 网站设计页面如何做居中网站建设开发价格