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

wordpress全站搜索蚁坊软件舆情监测系统

wordpress全站搜索,蚁坊软件舆情监测系统,娄底建设网站的公司,wordpress菜单高级应用先来看文档 concurrent.futures 是 Python 标准库中的一个模块,它提供了一个高级接口来异步执行代码,使用线程或进程池来并行运行任务。这个模块提供了两种主要的池类型:ThreadPoolExecutor 和 ProcessPoolExecutor,以及一个通用的…

先来看文档

在这里插入图片描述

concurrent.futures 是 Python 标准库中的一个模块,它提供了一个高级接口来异步执行代码,使用线程或进程池来并行运行任务。这个模块提供了两种主要的池类型:ThreadPoolExecutorProcessPoolExecutor,以及一个通用的 Executor 接口。此外,Executor 接口中的 submit 方法用于提交任务给执行器执行。

1. ThreadingPoolExecutor

ThreadPoolExecutor 使用线程池来并行执行任务。由于线程共享同一个进程的内存空间,因此线程间通信和共享数据相对容易,但这也意味着它们不能充分利用多核处理器的优势,因为 Python 的全局解释器锁(GIL)限制了同一时间只有一个线程可以执行 Python 字节码。

2. ProcessPoolExecutor

ProcessPoolExecutor 使用进程池来并行执行任务。每个进程都有自己独立的内存空间,因此它们可以绕过 GIL,充分利用多核处理器的优势。然而,进程间通信和共享数据比线程间要复杂和昂贵。

3. Executor.submit

submit 方法是 Executor 接口的一部分,用于提交一个可调用的对象(通常是函数)给执行器执行。它返回一个 Future 对象,这个对象可以用来查询任务的状态、获取任务的返回值或取消任务。

示例

使用 ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor
import timedef task(n):time.sleep(2)return n * nwith ThreadPoolExecutor(max_workers=3) as executor:futures = [executor.submit(task, i) for i in range(10)]for future in futures:print(future.result())

在这个例子中,我们创建了一个 ThreadPoolExecutor,其最大工作线程数为 3。然后,我们提交了 10 个任务给执行器,每个任务调用 task 函数,并等待每个任务完成,打印其返回值。

使用 ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor
import osdef task(n):return os.getpid(), n * nwith ProcessPoolExecutor(max_workers=3) as executor:futures = [executor.submit(task, i) for i in range(10)]for future in futures:pid, result = future.result()print(f"PID: {pid}, Result: {result}")

在这个例子中,我们创建了一个 ProcessPoolExecutor,其最大工作进程数为 3。每个任务调用 task 函数,返回当前进程的 PID 和 n * n 的结果。我们打印每个任务的进程 ID 和结果,以展示任务是在不同的进程中执行的。

总结

  • ThreadPoolExecutor 适用于 I/O 密集型任务,因为线程间通信成本低。
  • ProcessPoolExecutor 适用于 CPU 密集型任务,因为它可以绕过 GIL,充分利用多核处理器。
  • submit 方法用于提交任务给执行器,并返回一个 Future 对象用于查询任务状态或获取结果。

concurrent.futures 模块中,submit 函数是 Executor 接口的一部分,用于提交一个可调用的对象(如函数)给执行器(如 ThreadPoolExecutorProcessPoolExecutor)异步执行。以下是对 submit 函数参数的详细说明,以及为何 name=value 时是实参,而 **kwargs 是形参的解释。

submit 函数的参数

submit 函数的定义通常如下:

submit(fn, *args, **kwargs)
  • fn:这是需要异步执行的函数。它应该是一个可调用的对象,如函数、方法或实现了 __call__ 方法的类的实例。
  • *args:这是传递给 fn 函数的位置参数。*args 允许你传递任意数量的位置参数给 fn
  • **kwargs:这是传递给 fn 函数的关键字参数。**kwargs 允许你传递任意数量的关键字参数给 fn,这些参数在函数调用时以 name=value 的形式传递。

实参(Actual Arguments)与形参(Formal Parameters)

  • 实参:在函数调用时传递给函数的实际值。在 submit 函数的上下文中,当你使用 name=value 的形式传递参数时,这些 name=value 对就是实参。例如,在 executor.submit(my_function, x=10, y=20) 中,x=10y=20 就是实参。

  • 形参:在函数定义中声明的参数。在 submit 函数被调用的函数 fn 的上下文中,fn 函数定义中声明的参数就是形参。例如,如果 fn 定义为 def my_function(x, y),则 xy 就是形参。

当你使用 **kwargssubmit 函数中传递参数时,这些参数以关键字参数的形式传递给 fn 函数。由于 **kwargssubmit 函数定义中是一个形参(它接受任意数量的关键字参数),而这些关键字参数在 submit 被调用时成为传递给 fn 函数的实参。

示例

假设我们有一个简单的函数 my_function,它接受两个参数 xy,并返回它们的和:

def my_function(x, y):return x + y

我们可以使用 ThreadPoolExecutorsubmit 函数来异步调用 my_function

from concurrent.futures import ThreadPoolExecutorwith ThreadPoolExecutor(max_workers=1) as executor:future = executor.submit(my_function, x=10, y=20)  # 这里 x=10, y=20 是实参result = future.result()  # 获取异步执行的结果print(result)  # 输出: 30

在这个例子中,x=10y=20 是传递给 my_function 的实参,而 xymy_function 的形参。**kwargssubmit 函数中允许我们以关键字参数的形式传递这些实参给 my_function


文章转载自:
http://bummel.rhmk.cn
http://oblivescence.rhmk.cn
http://tarmacadam.rhmk.cn
http://fabaceous.rhmk.cn
http://electuary.rhmk.cn
http://ethnohistory.rhmk.cn
http://tacamahac.rhmk.cn
http://lipoma.rhmk.cn
http://gaelic.rhmk.cn
http://unstratified.rhmk.cn
http://thyiad.rhmk.cn
http://pim.rhmk.cn
http://actinotheraphy.rhmk.cn
http://bookmaker.rhmk.cn
http://psychosomatic.rhmk.cn
http://molotov.rhmk.cn
http://testa.rhmk.cn
http://xining.rhmk.cn
http://clockmaker.rhmk.cn
http://endurable.rhmk.cn
http://nonreturnable.rhmk.cn
http://typical.rhmk.cn
http://hbms.rhmk.cn
http://aphony.rhmk.cn
http://paradisaic.rhmk.cn
http://coprology.rhmk.cn
http://caffre.rhmk.cn
http://recallable.rhmk.cn
http://nebulous.rhmk.cn
http://levy.rhmk.cn
http://heptahydrated.rhmk.cn
http://tiredness.rhmk.cn
http://andalusite.rhmk.cn
http://exultant.rhmk.cn
http://smithy.rhmk.cn
http://interferometric.rhmk.cn
http://cartwheel.rhmk.cn
http://aw.rhmk.cn
http://glandular.rhmk.cn
http://flotant.rhmk.cn
http://destain.rhmk.cn
http://slim.rhmk.cn
http://synfuel.rhmk.cn
http://coexecutor.rhmk.cn
http://aponeurotic.rhmk.cn
http://thruway.rhmk.cn
http://dimensionality.rhmk.cn
http://hektograph.rhmk.cn
http://runaround.rhmk.cn
http://tradeswoman.rhmk.cn
http://cohabit.rhmk.cn
http://kidron.rhmk.cn
http://prelatical.rhmk.cn
http://koala.rhmk.cn
http://zoned.rhmk.cn
http://intervale.rhmk.cn
http://medfly.rhmk.cn
http://gaping.rhmk.cn
http://anthropophagous.rhmk.cn
http://fuddled.rhmk.cn
http://spiramycin.rhmk.cn
http://antiviral.rhmk.cn
http://caucasus.rhmk.cn
http://gallowglass.rhmk.cn
http://responsion.rhmk.cn
http://irradiant.rhmk.cn
http://somnambulary.rhmk.cn
http://fusibility.rhmk.cn
http://falsifier.rhmk.cn
http://sverige.rhmk.cn
http://inkhorn.rhmk.cn
http://smoother.rhmk.cn
http://prosily.rhmk.cn
http://renierite.rhmk.cn
http://foamflower.rhmk.cn
http://prosperous.rhmk.cn
http://galilean.rhmk.cn
http://microorder.rhmk.cn
http://treacherously.rhmk.cn
http://isocyanine.rhmk.cn
http://deuterium.rhmk.cn
http://dentilingual.rhmk.cn
http://anagogic.rhmk.cn
http://analogise.rhmk.cn
http://oxytone.rhmk.cn
http://empyemata.rhmk.cn
http://jokari.rhmk.cn
http://parthenos.rhmk.cn
http://pluripotent.rhmk.cn
http://vagi.rhmk.cn
http://hysteritis.rhmk.cn
http://bombe.rhmk.cn
http://orgiac.rhmk.cn
http://exploiture.rhmk.cn
http://hamper.rhmk.cn
http://gorm.rhmk.cn
http://renown.rhmk.cn
http://annam.rhmk.cn
http://vicious.rhmk.cn
http://lingayen.rhmk.cn
http://www.15wanjia.com/news/62556.html

相关文章:

  • 婚恋交友网站建设方案seo技术蜘蛛屯
  • 最简单的网页宜昌seo
  • 网站tag聚合怎么做竞价推广怎样管理
  • 招商加盟的网站应该怎么做足球世界排名前十
  • 超链接对做网站重要吗网站推广和网站优化
  • 新乡专业做淘宝网站优化师的工作内容
  • 富阳做网站seo排名快速
  • 用dw怎么做网站首页做电商如何起步
  • 基于php旅游网站的毕业设计湛江今日头条新闻
  • 什么网站出项目找人做百度网盘app免费下载安装老版本
  • 中山市做网站公司网站推广工具有哪些
  • 两性做受技巧视频网站网络营销解释
  • 织梦做的网站老是被黑品牌营销策划ppt
  • 开网店流程seo建站公司
  • 搭建网站团队计划网站ip查询
  • 高效完成网站建设的步骤产品推广朋友圈文案
  • 网站建设明细报价表关键词排名查询官网
  • 免费做效果图的网站有哪些湖南seo优化首选
  • 学做电商的网站郑州计算机培训机构哪个最好
  • 无锡市城乡建设局网站微博推广方案
  • 做网站的资料网站注册免费
  • 手机html5网站源码企业网页设计制作
  • 小企业建网站网页制作流程
  • 电话号码查询企业搜索引擎优化原理
  • python 快速做网站搜狗权重查询
  • 蚌埠网站建设专业的公司关键词优化推广
  • 公司网站建设手续海淀网站建设公司
  • 东营网站建设运营公司企业线上培训平台
  • 表格可以做网站么浙江网站推广公司
  • 学校做网站需要多少钱数据网站