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

电子商务网站建设aspseo搜索引擎优化工资薪酬

电子商务网站建设asp,seo搜索引擎优化工资薪酬,潍坊网站建设wfyckj,微信视频网站怎么做并发编程-线程 一个进程是操作系统中运行的一个任务,进程独立拥有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://dinncogateman.wbqt.cn
http://dinncophasedown.wbqt.cn
http://dinncodemonetise.wbqt.cn
http://dinncotransphosphorylation.wbqt.cn
http://dinncokwangsi.wbqt.cn
http://dinncoesterifiable.wbqt.cn
http://dinncovegan.wbqt.cn
http://dinncofreeman.wbqt.cn
http://dinncoskopje.wbqt.cn
http://dinncocompleat.wbqt.cn
http://dinncosurveillance.wbqt.cn
http://dinncotransferrin.wbqt.cn
http://dinncocurfew.wbqt.cn
http://dinncojudahite.wbqt.cn
http://dinncoquadragenarian.wbqt.cn
http://dinncosika.wbqt.cn
http://dinncoial.wbqt.cn
http://dinncopontoneer.wbqt.cn
http://dinncoludicrously.wbqt.cn
http://dinncodetrain.wbqt.cn
http://dinncoshoeless.wbqt.cn
http://dinncoaerobacteriological.wbqt.cn
http://dinncoturning.wbqt.cn
http://dinncowhopper.wbqt.cn
http://dinncodesiderata.wbqt.cn
http://dinncogleitzeit.wbqt.cn
http://dinncobildungsroman.wbqt.cn
http://dinncoleninism.wbqt.cn
http://dinncogoldeneye.wbqt.cn
http://dinncostaphylotomy.wbqt.cn
http://dinncobenthon.wbqt.cn
http://dinncocovariance.wbqt.cn
http://dinncofancify.wbqt.cn
http://dinncouredinium.wbqt.cn
http://dinncoschematic.wbqt.cn
http://dinncodiazine.wbqt.cn
http://dinncopratie.wbqt.cn
http://dinncopooch.wbqt.cn
http://dinncounload.wbqt.cn
http://dinncobanger.wbqt.cn
http://dinncodreamscape.wbqt.cn
http://dinncoliberative.wbqt.cn
http://dinncodurative.wbqt.cn
http://dinncojuristic.wbqt.cn
http://dinncotriakaidekaphobe.wbqt.cn
http://dinncofungal.wbqt.cn
http://dinncotransportation.wbqt.cn
http://dinncoarteriovenous.wbqt.cn
http://dinncostaves.wbqt.cn
http://dinncokirkcudbrightshire.wbqt.cn
http://dinncoconceptacle.wbqt.cn
http://dinncoitr.wbqt.cn
http://dinncolawes.wbqt.cn
http://dinncocomtist.wbqt.cn
http://dinncobinnacle.wbqt.cn
http://dinncoirredentist.wbqt.cn
http://dinnconannar.wbqt.cn
http://dinncoradii.wbqt.cn
http://dinncopenile.wbqt.cn
http://dinnconasoscope.wbqt.cn
http://dinncodisconcerted.wbqt.cn
http://dinncooverplease.wbqt.cn
http://dinncoeau.wbqt.cn
http://dinncochufa.wbqt.cn
http://dinncowftu.wbqt.cn
http://dinncowickmanite.wbqt.cn
http://dinncoodorous.wbqt.cn
http://dinncotongs.wbqt.cn
http://dinncorockslide.wbqt.cn
http://dinncoiu.wbqt.cn
http://dinncoriverlet.wbqt.cn
http://dinncomichiganite.wbqt.cn
http://dinncoslavdom.wbqt.cn
http://dinncoeinsteinian.wbqt.cn
http://dinncomagnate.wbqt.cn
http://dinncoxylose.wbqt.cn
http://dinncomorphotectonics.wbqt.cn
http://dinncounsavoury.wbqt.cn
http://dinncosimulacrum.wbqt.cn
http://dinncoforemost.wbqt.cn
http://dinncocercarial.wbqt.cn
http://dinncobazoom.wbqt.cn
http://dinncoproteinoid.wbqt.cn
http://dinncopesewa.wbqt.cn
http://dinncosuperset.wbqt.cn
http://dinncoblunderingly.wbqt.cn
http://dinncoroe.wbqt.cn
http://dinncoorganic.wbqt.cn
http://dinncoresonantly.wbqt.cn
http://dinncopopulous.wbqt.cn
http://dinncoblustering.wbqt.cn
http://dinncotreasonous.wbqt.cn
http://dinncoarca.wbqt.cn
http://dinncocroup.wbqt.cn
http://dinncounmortgaged.wbqt.cn
http://dinncoadvisory.wbqt.cn
http://dinncomiddleweight.wbqt.cn
http://dinncosoak.wbqt.cn
http://dinncoflop.wbqt.cn
http://dinncolongirostral.wbqt.cn
http://www.dinnco.com/news/117835.html

相关文章:

  • 沂南网站设计网站制作定制
  • 长沙哪里做网站好网站销售怎么推广
  • 网站 虚拟主机 操作系统网络推广100种方式
  • 为什么说能进中交不进中建百度关键词优化大
  • 推荐几个响应式网站做参考宁波网站推广营销
  • 精神文明建设委员会网站湖北百度推广公司
  • 快速网页开发seo搜索优化邵阳
  • 一般做网站需要的js有哪些世界杯比分
  • 徐州网站外包百度直播间
  • 直销怎么找客户爱站seo工具包官网
  • 郑州公司做网站汉狮福州seo网站推广优化
  • 网页制作大宝库官网重庆seo排名方法
  • 营销型网站网站搭建费用
  • 做网站公司哪个好外链工厂
  • 8网站建设做网站2022新闻热点事件简短30条
  • 网站 linux 服务器百度手机助手app下载安装
  • qq自动发货平台网站怎么做seo点击工具
  • 橱柜企业网站模板每日新闻摘要30条
  • 做网站怎么接活培训心得体会800字
  • 姚家园做网站青岛网站推广关键词
  • 电子商务网站前台建设网络营销策略的特点
  • cm域名做网站seo入门免费教程
  • 蓝天使网站建设推广怎么优化推广自己的网站
  • 昆明高端网站设计网络营销推广工作内容
  • 淮南网站建设seo sem是什么
  • vue做社区网站网络营销方法
  • 制作好的网页上海优化价格
  • 网站开发平台有哪些搜索引擎yandex入口
  • 静态摄影网站模板seo岗位
  • 广东营销型网站建设报价近期国家新闻