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

腾讯官方网站做书签/自己怎么做引流推广

腾讯官方网站做书签,自己怎么做引流推广,北京活动网站制作,网站建设购物车一、concurrent.futures模块简介 concurrent.futures 模块提供了并发执行调用的高级接口 并发可以使用threads执行,使用ThreadPoolExecutor 或 分离的processes,使用ProcessPoolExecutor。都实现了同一个接口,这个接口在抽象类Executor定义 二…

一、concurrent.futures模块简介

concurrent.futures 模块提供了并发执行调用的高级接口

并发可以使用threads执行,使用ThreadPoolExecutor 或 分离的processes,使用ProcessPoolExecutor。都实现了同一个接口,这个接口在抽象类Executor定义

二、类的属性和方法

concurrent.futures.wait(fstimeout=Nonereturn_when=ALL_COMPLETED):wait等待fs里面所有的Future实例(由不同的Executors实例创建的)完成。返回两个命名元祖,第一个元祖名为done,存放完成的futures对象,第二个元祖名为not_done,存放未完成的futures。return_when参数必须是concurrent.futures里面定义的常量:FIRST_COMPLETED,FIRST_EXCEPTION,ALL_COMPLETED

concurrent.futures.as_completed(fstimeout=None):返回一个迭代器,yield那些完成的futures对象。fs里面有重复的也只可能返回一次。任何futures在调用as_completed()调用之前完成首先被yield。

三、Future对象

 Future()封装了可调用对象的异步执行。Future实例可以被Executor.submit()方法创建。除了测试之外不应该直接创建。Future对象可以和异步执行的任务进行交互

cancel():尝试去取消调用。如果调用当前正在执行,不能被取消。这个方法将返回False,否则调用将会被取消,方法将返回Truecancelled():如果调用被成功取消返回Truerunning():如果当前正在被执行不能被取消返回Truedone():如果调用被成功取消或者完成running返回Trueresult(Timeout = None):拿到调用返回的结果。如果没有执行完毕就会去等待exception(timeout=None):捕获程序执行过程中的异常add_done_callback(fn):将fn绑定到future对象上。当future对象被取消或完成运行时,fn函数将会被调用以下的方法是在unitest中set_running_or_notify_cancel()set_result(result)set_exception(exception) 
Future方法

四、Executor对象

1、抽象类,提供异步调用的方法。不能被直接使用,而是通过构建子类。

2、方法

提交任务方式一:submit(fn*args**kwargs):调度函数fn(*args **kwargs)返回一个Future对象代表调用的执行。

提交任务方式二:map(func*iterablestimeout=Nonechunksize=1):和map(func, *iterables)相似。但是该map方法的执行是异步的。多个func的调用可以同时执行。当Executor对象是 ProcessPoolExecutor,才可以使用chunksize,将iterable对象切成块,将其作为分开的任务提交给pool,默认为1。对于很大的iterables,设置较大chunksize可以提高性能(切记)。

shutdown(wait=True):给executor发信号,使其释放资源,当futures完成执行时。已经shutdown再调用submit()或map()会抛出RuntimeError。使用with语句,就可以避免必须调用本函数

五、ThreadPoolExecutor对象

ThreadPoolExecutor是Executor的子类使用线程池来异步执行调用

如果使用不正确可能会造成死锁,所以submit的task尽量不要调用executor和futures,否则很容易出现死锁

import time
def wait_on_b():time.sleep(5)print(b.result())  # b will never complete because it is waiting on a.return 5def wait_on_a():time.sleep(5)print(a.result())  # a will never complete because it is waiting on b.return 6executor = ThreadPoolExecutor(max_workers=2)
a = executor.submit(wait_on_b)
b = executor.submit(wait_on_a)
相互等待的死锁
def wait_on_future():f = executor.submit(pow, 5, 2)# This will never complete because there is only one worker thread and# it is executing this function.print(f.result())executor = ThreadPoolExecutor(max_workers=1)
executor.submit(wait_on_future)
等待自己的结果的死锁

默认的max_workers是设备的处理器数目*5

六、ProcessPoolExecutor对象

 ProcessPoolExecutor同样是Executor的子类。使用进程池来异步执行调用。

Executor.submit() called:
- creates a uniquely numbered _WorkItem and adds it to the "Work Items" dict
- adds the id of the _WorkItem to the "Work Ids" queueLocal worker thread:
- reads work ids from the "Work Ids" queue and looks up the correspondingWorkItem from the "Work Items" dict: if the work item has been cancelled thenit is simply removed from the dict, otherwise it is repackaged as a_CallItem and put in the "Call Q". New _CallItems are put in the "Call Q"until "Call Q" is full. NOTE: the size of the "Call Q" is kept small becausecalls placed in the "Call Q" can no longer be cancelled with Future.cancel().
- reads _ResultItems from "Result Q", updates the future stored in the"Work Items" dict and deletes the dict entryProcess #1..n:
- reads _CallItems from "Call Q", executes the calls, and puts the resulting_ResultItems in "Result Q"
数据流程解释

 

ProcessPoolExecutor使用multiprocessing模块,不受GIL锁的约束,意味着只有可以pickle的对象才可以执行和返回(pickle参考)

__main__必须能够被工作子进程导入。所以意味着ProcessPoolExecutor在交互式解释器下不能工作。

提交给ProcessPoolExecutor的可调用方法里面调用Executor或Future将会形成死锁。

class concurrent.futures.ProcessPoolExecutor(max_workers=None)

max_workers默认是处理器的个数

import concurrent.futures
import mathPRIMES = [112272535095293,112582705942171,112272535095293,115280095190773,115797848077099,115797848077098,1099726899285419]def is_prime(n):"""to judge the input number is prime or not:param n: input number:return: True or False"""if n % 2 == 0:return Falsesqrt_n = int(math.(math.sqrt(n)))for i in range(3,sqrt_n + 1, 2):if n % i == 0:return Falsereturn Truedef main():"""create Process Pool to judge the numbers is prime or not:return: None"""with concurrent.futures.ProcessPoolExecutor() as executor:for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):print(number,prime)if __name__ == '__main__':main()
样例

七、Exception类

exception concurrent.futures.CancelledError

exception concurrent.futures.TimeoutError

exception concurrent.futures.process.BrokenProcessPool

 

转载于:https://www.cnblogs.com/skiler/p/7080179.html

http://www.lbrq.cn/news/770275.html

相关文章:

  • 网站开发与app差距/网站流量分析报告
  • 建设电商网站的个人心得/企业营销网站建设系统
  • 拉销智能模板建站系统/抖音关键词排名系统
  • 仪征网站建设/友情链接发布平台
  • 建立一个网站多少钱/网红推广团队去哪里找
  • 网站产品要如何做详情/seo网站推广企业
  • 京东网站建设策略/怎样在百度打广告
  • 哪些网站做平面设计素材/长沙网站推广排名优化
  • 宁波网站建设设计制作/福州seo外包公司
  • 公司网站开发外包公司/百度app交易平台
  • 手机响应式网站开发模板之家/自己怎么制作网站
  • 浦元品牌网站建设/百度竞价点击一次多少钱
  • 网页建站系统/百度快照
  • 想学做网站 应该学/全国疫情突然又严重了
  • 如何编程做网站/被逆冬seo课程欺骗了
  • 继电器做网站/开发网站用什么软件
  • 网站备案最快几天/2022年新闻热点事件
  • 安徽网站建设详细策划/免费网络推广工具
  • 企业网站建设设计公司/职业培训热门行业
  • 建设企业网站是静态还是动态好/兰州网络推广
  • 免费扑克网站代码/新开传奇网站
  • 青岛网站制作价格/怎样建立一个网站
  • 手表网站背景素材/青岛做网站的公司哪家好
  • 云霄县建设局网站投诉/2022年最新新闻播报稿件
  • wordpress数据库损坏网站/武汉刚刚发生的新闻
  • wordpress 商城模版/网站关键词优化排名技巧
  • 建设网站前端/seo外链
  • 如何评判一个网站建设的怎么样/百度百度一下就知道
  • 阿里巴巴做国际网站要多少钱/灰色词seo推广
  • 政府单位建设网站的申请报告/外贸企业网站制作哪家好
  • 基于js和html的点名应用
  • 三、非线性规划
  • 挂糊:给食材穿层 “黄金保护衣”
  • 基于DDPG的车辆纵向速度控制优化:兼顾速度与乘坐舒适性
  • Query通过自注意力机制更新(如Transformer解码器的自回归生成)的理解
  • 机器翻译:学习率调度详解