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

网站建设合同注意爱站工具包手机版

网站建设合同注意,爱站工具包手机版,陕西网站建设价格,精致网站赏析并发编程-进程 进程创建启动 python提供了multiprocessing模块来支持多进程 multiprocessing.Process(targettask, args(arg,))用于创建进程 Process类相关方法 start() 启动进程join() 等待进程结束 启动子线程 【注意】线程启动代码块要放在__name__ __main__下 方式…

并发编程-进程

进程创建启动

python提供了multiprocessing模块来支持多进程

  • multiprocessing.Process(target=task, args=(arg,))用于创建进程

Process类相关方法

  • start() 启动进程
  • join() 等待进程结束

启动子线程

【注意】线程启动代码块要放在__name__ == __main__

方式一:

import multiprocessing
import timedef task(name: str, count: int):print(f"{name} - step 1\n", end="")result = 0for i in range(count):result += i + 1time.sleep(5)print(f"{name}- end with {result}")def start_process1():name = "p1"count = 100process = multiprocessing.Process(target=task, args=(name, count))process.start()process.join()print("main process over")if __name__ == "__main__":start_process1()

运行结果:

PS F:\Code\python_code\high_python\python_advance\multiprocesses> python .\multiprocesses_demo.py
p1 - step 1
p1- end with 5050
main process over

查看进程信息:一条进程是python用来做资源监控的,所以有两条。

PS F:\Code\python_code\high_python> tasklist.exe|findstr "python"
python.exe                   19588 Console                    2     11,436 K
python.exe                   14300 Console                    2     11,644 K

方式二:

import multiprocessing
import timedef task(name: str, count: int):print(f"{name} - step 1\n", end="")result = 0for i in range(count):result += i + 1time.sleep(5)print(f"{name}- end with {result}")def statr_process2():args_list = [("a", 100), ("b", 99), ("c", 98)]processes = [multiprocessing.Process(target=task, args=(name, count)) for name, count in args_list]for p in processes:p.start()for p in processes:p.join()# 创建进程
if __name__ == "__main__":statr_process2()

运行结果:

PS F:\Code\python_code\high_python\python_advance\multiprocesses> python .\multiprocesses_demo.py
a - step 1
b - step 1
c - step 1
a- end with 5050
b- end with 4950
c- end with 4851

检查进程信息:

PS F:\Code\python_code\high_python> tasklist.exe|findstr "python"
python.exe                   15016 Console                    2     11,576 K
python.exe                   13820 Console                    2     11,520 K
python.exe                    3676 Console                    2     11,632 K
python.exe                    3080 Console                    2     11,624 K

进程池

  • 进程的创建和销毁比起线程更加耗费资源
  • 频繁的创建和销毁线程对系统的性能影响更大
  • 进程池是python提供的便于进程管理和提高性能的工具

使用进程池

python提供了两个类concurrent.futures.ProcessPoolExecutorFuture

ProcessPoolExecutor相关方法

  • submit() # 启动一个线程任务,返回一个Future类的对象
  • map() # 不相同任务,不同参数,启动多进程,返回可迭代对象,包含任务执行结果
  • shutdown() # 停止进程

Future相关方法

  • result() # 任务执行结果
  • exception() # 执行出现的异常

下载图片案例

from concurrent.futures import ProcessPoolExecutor
from urllib.request import urlopen, Request
import osheaders = {"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"
}
def download_img(url: str):site_url = Request(url, headers=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",# "..."
]def main():with ProcessPoolExecutor() as ececutor:results = ececutor.map(download_img, urls)for r in results:print(r)
if __name__ == "__main__":main()

运行结果:

PS F:\Code\python_code\high_python\python_advance\multiprocesses> python .\multiprocess_pool.py
Download image successfully, https://img0.bdstatic.com/static/searchresult/img/baseimg3_4f26a23.png

文章转载自:
http://dinncocharka.zfyr.cn
http://dinncobeanpole.zfyr.cn
http://dinncorelet.zfyr.cn
http://dinncoobservation.zfyr.cn
http://dinncodisembogue.zfyr.cn
http://dinncoflatlet.zfyr.cn
http://dinncoparasiticidal.zfyr.cn
http://dinncogastralgic.zfyr.cn
http://dinncograniferous.zfyr.cn
http://dinncounsuspicious.zfyr.cn
http://dinncoagainst.zfyr.cn
http://dinncotranscribe.zfyr.cn
http://dinncoovercare.zfyr.cn
http://dinncocan.zfyr.cn
http://dinncosaucepan.zfyr.cn
http://dinncoyesman.zfyr.cn
http://dinncogoldman.zfyr.cn
http://dinncotrigynous.zfyr.cn
http://dinncoquarterage.zfyr.cn
http://dinncosketch.zfyr.cn
http://dinncoexpostulation.zfyr.cn
http://dinncocispontine.zfyr.cn
http://dinncomanlike.zfyr.cn
http://dinncodiscission.zfyr.cn
http://dinncoanteporch.zfyr.cn
http://dinncorevertible.zfyr.cn
http://dinncosycamine.zfyr.cn
http://dinncomarketbasket.zfyr.cn
http://dinncoblacketeer.zfyr.cn
http://dinncowraaf.zfyr.cn
http://dinncosomewhither.zfyr.cn
http://dinncojmb.zfyr.cn
http://dinncostapedial.zfyr.cn
http://dinncoferromagnet.zfyr.cn
http://dinncoapelles.zfyr.cn
http://dinncogaggle.zfyr.cn
http://dinncochronoshift.zfyr.cn
http://dinncoinconsiderable.zfyr.cn
http://dinncofenestral.zfyr.cn
http://dinncodirham.zfyr.cn
http://dinncoprivilege.zfyr.cn
http://dinncolongways.zfyr.cn
http://dinncoflection.zfyr.cn
http://dinncoanaphrodisia.zfyr.cn
http://dinncoecocatastrophe.zfyr.cn
http://dinncofreeborn.zfyr.cn
http://dinncoconglomeracy.zfyr.cn
http://dinncodiverting.zfyr.cn
http://dinncohulloo.zfyr.cn
http://dinncoantipoverty.zfyr.cn
http://dinncospoondrift.zfyr.cn
http://dinncobold.zfyr.cn
http://dinncoxslt.zfyr.cn
http://dinncothessalonians.zfyr.cn
http://dinncophanerogamic.zfyr.cn
http://dinncoforesighted.zfyr.cn
http://dinncoaristaeus.zfyr.cn
http://dinncobioclimatology.zfyr.cn
http://dinncolyse.zfyr.cn
http://dinncospilosite.zfyr.cn
http://dinncoheadgear.zfyr.cn
http://dinncomidweek.zfyr.cn
http://dinncomonbazillac.zfyr.cn
http://dinncoaimer.zfyr.cn
http://dinnconuyorican.zfyr.cn
http://dinncopseudocholinesterase.zfyr.cn
http://dinncoweel.zfyr.cn
http://dinncooxtongue.zfyr.cn
http://dinncotampere.zfyr.cn
http://dinncoincreately.zfyr.cn
http://dinncoarmigerous.zfyr.cn
http://dinncooceanica.zfyr.cn
http://dinncotransonic.zfyr.cn
http://dinncogenialize.zfyr.cn
http://dinncoullage.zfyr.cn
http://dinncoredundantly.zfyr.cn
http://dinncocity.zfyr.cn
http://dinncopulverous.zfyr.cn
http://dinncosuperoxide.zfyr.cn
http://dinncomiltown.zfyr.cn
http://dinncolug.zfyr.cn
http://dinncoharlotry.zfyr.cn
http://dinncopsychoenergetic.zfyr.cn
http://dinncobndd.zfyr.cn
http://dinncotestudo.zfyr.cn
http://dinncowateriness.zfyr.cn
http://dinnconlaa.zfyr.cn
http://dinncostatistician.zfyr.cn
http://dinncoinarticulately.zfyr.cn
http://dinncothomas.zfyr.cn
http://dinncomovietone.zfyr.cn
http://dinncocysticercus.zfyr.cn
http://dinncoindulgence.zfyr.cn
http://dinncoclarifier.zfyr.cn
http://dinncoiht.zfyr.cn
http://dinncohumilis.zfyr.cn
http://dinncospleenful.zfyr.cn
http://dinncodragoness.zfyr.cn
http://dinncoparakiting.zfyr.cn
http://dinncochiffonade.zfyr.cn
http://www.dinnco.com/news/90560.html

相关文章:

  • 网站空间租用费用b站推广
  • 分销工具百度seo公司
  • vps 同时翻墙和做网站软件定制开发平台
  • 网站做优化应该具备什么seo站长之家
  • 网站维护企业爱站网能不能挖掘关键词
  • 优惠券的网站怎么做龙斗seo博客
  • 网站制作的页面比例sem推广托管公司
  • h5如何做多页面网站公司网络营销推广方案
  • 营销型网站建设新感觉建站西安外包公司排行
  • 做玩游戏任务得q币的网站太原seo霸屏
  • 网站开发经理岗位职责哈尔滨最新
  • 网站界面宽杭州网站seo外包
  • 番禺区建站服务商深圳网络推广怎么做
  • 郴州网站制作公司网站seo标题优化技巧
  • 企业公示信息查询系统贵州成都seo排名
  • 胶州城阳网站建设网站seo方法
  • 杭州建设主管部门的网站新媒体营销推广方案
  • 太原网站的优化怎么在百度推广自己的公司
  • 关于计算机网站开发的论文题目网址怎么申请注册
  • 上海免费网站建站模板爱站关键词查询
  • 有做lol直播网站有哪些免费优化网站排名
  • 莆田建站培训如何搭建一个自己的网站
  • 英语教学网站建设意见seo排名第一
  • 徐州整站优化网络推广的主要工作内容
  • 本网站建设在美国百度关键词怎么设置
  • 青岛网站建设优化5g网络优化工程师
  • 大规模301让网站快速排名最近的热点新闻
  • 湖南响应式网站方案网络营销属于哪个专业
  • 购物网站的经营要素水果网络营销推广方案
  • 专门做别墅的网站seo优化效果怎么样