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

网站开发的账务处理线上营销有哪些

网站开发的账务处理,线上营销有哪些,做网站的模仿还要去量宽高吗,做文案的网站1. 线程池参数设置 CPU数量:N线程池的核心线程数量 IO密集型的话,一般设置为 2 * N 1; CPU密集型的话,一般设置为 N 1 或者 使用进程池。线程池的最大任务队列长度 (线程池的核心线程数 / 单个任务的执行时间&#…

1. 线程池参数设置

  1. CPU数量:N
  2. 线程池的核心线程数量
    IO密集型的话,一般设置为 2 * N + 1
    CPU密集型的话,一般设置为 N + 1 或者 使用进程池。
  3. 线程池的最大任务队列长度
    (线程池的核心线程数 / 单个任务的执行时间)* 2
    如果线程池有10个核心线程,单个任务的执行时间为0.1s,那么最大任务队列长度设置为200。
from concurrent.futures import ThreadPoolExecutor
thread_pool = ThreadPoolExecutor(max_workers=10)

2. submit方式提交

submit 这种提交方式是一条一条地提交任务:
1. 可以提交不同的任务函数;
2. 线程池的线程在执行任务时出现异常,程序不会停止,而且也看不到对应的报错信息;
3. 得到的结果是乱序的。

import time
from concurrent.futures import ThreadPoolExecutor, as_completeddef run_task(delay):print(f"------------> start to execute task {delay} <------------")time.sleep(delay)print(f"------------> task {delay} execute over !!! <------------")return delay + 10000task_params = [1, 4, 2, 5, 3, 6] * 10
threadpool_max_worker = 10      # io密集型:cpu数量*2+1;cpu密集型:cpu数量+1
thread_pool = ThreadPoolExecutor(max_workers=threadpool_max_worker)############################### 方式1. 虽然是异步提交任务,但是却是同步执行任务。
for p in task_params:future = thread_pool.submit(run_task, p)print(future.result())      # 直接阻塞当前线程,直到任务完成并返回结果,即变成同步############################### 方式2. 异步提交任务,而且异步执行任务,乱序执行,结果乱序。
future_list = []
for p in task_params:future = thread_pool.submit(run_task, p)future_list.append(future)for res in as_completed(future_list):       # 等待子线程执行完毕,先完成的会先打印出来结果,结果是无序的print(f"get last result is {res.result()}")

3. map方式提交

map 这种提交方式可以分批次提交任务:

  1. 每个批次提价的任务函数都相同;
  2. 线程池的线程在执行任务时出现异常,程序终止并打印报错信息;
  3. 得到的结果是有序的。
import time
from concurrent.futures import ThreadPoolExecutor, as_completeddef run_task(delay):print(f"------------> start to execute task {delay} <------------")time.sleep(delay)print(f"------------> task {delay} execute over !!! <------------")return delay + 10000task_params = [1, 4, 2, 5, 3, 6] * 10
threadpool_max_worker = 5      # io密集型:cpu数量*2+1;cpu密集型:cpu数量+1
thread_pool = ThreadPoolExecutor(max_workers=threadpool_max_worker)task_res = thread_pool.map(run_task, task_params)		# 批量提交任务,乱序执行
print(f"main thread run finished!")
for res in task_res:		# 虽然任务是乱序执行的,但是得到的结果却是有序的。print(f"get last result is {res}")

4. 防止一次性提交的任务量过多

import time
from concurrent.futures import ThreadPoolExecutordef run_task(delay):print(f"------------> start to execute task <------------")time.sleep(delay)print(f"------------> task execute over !!! <------------")task_params = [1, 4, 2, 5, 3, 6] * 100
threadpool_max_worker = 10      # io密集型:cpu数量*2+1;cpu密集型:cpu数量+1
thread_pool = ThreadPoolExecutor(max_workers=threadpool_max_worker)
threadpool_max_queue_size = 200     # 线程池任务队列长度一般设置为  (线程池核心线程数/单个任务执行时间)* 2for p in task_params:print(f"*****************> 1. current queue size of thread pool is {thread_pool._work_queue.qsize()}")while thread_pool._work_queue.qsize() >= threadpool_max_queue_size:time.sleep(1)		# sleep时间要超过单个任务的执行时间print(f"*****************> 2. current queue size of thread pool is {thread_pool._work_queue.qsize()}")thread_pool.submit(run_task, p)print(f"main thread run finished!")

5. 案例分享

案例背景:由于kafka一个topic的一个分区数据只能由一个消费者组中的一个消费者消费,所以现在使用线程池,从kafka里消费某一个分区的数据,将数据提取出来并存于mysql或者redis,然后手动提交offset。

import time
import queue
import concurrent.futures
from threading import Thread
from concurrent.futures import ThreadPoolExecutordef send_task_to_queue(q, params):for idx, p in enumerate(params):q.put((idx, p))     # 把kafka数据put到queue里,如果queue满了就先阻塞着,等待第15行get数据后腾出空间,这里继续put数据print(f"\n set p: {p} into task queue, queue size is {q.qsize()}")def run_task(param_queue):idx, p = param_queue.get()      # 这里一直get数据,即使queue空了,只要kafka持续产生数据,第10行就会持续put数据到queue里print(f"\n ------------> start to execute task {idx} <------------")time.sleep(p)print(f"\n ------------> task {idx} execute over !!! <------------")return idxtask_params = [1, 4, 2, 5, 3] * 20      # 数据模拟kafka中消费得到的数据
thread_pool = ThreadPoolExecutor(max_workers=10)
task_param_queue = queue.Queue(maxsize=10)# 这里启动一个子线程一直往queue里put数据
thread_send_task = Thread(target=send_task_to_queue, args=(task_param_queue, task_params))
thread_send_task.start()while True:     # 这里为什么一直死循环:只要生产者生产数据存储在kafka中,那么消费者就一直能获取到数据future_list = []# 分批去消费queue里的数据for i in range(10):# 这里的子线程从queue里get任务后,queue腾出空间,上面的子线程继续往里面put数据future_list.append(thread_pool.submit(run_task, task_param_queue))# 子线程任务执行结束后,从结果里取最大的索引值,可以用于redis记录,并用户手动提交offset...complete_res, uncomplete_res = concurrent.futures.wait(future_list)future_max_idx = max([future_complete.result() for future_complete in complete_res])print(f"\n ######################################## every batch's max idx is {future_max_idx}")...		# 自行使用 future_max_idx 这个值做处理...

6. 案例优化

上面的案例,是将数据存储于线程队列中,保证每个子线程get()到的数据不重复。
既然线程队列可以保证每个子线程get到的数据不重复,那么利用生成器的一次性特性(使用完一次就没了),是不是也能达到这个效果呢?
试着优化下:

import time
import concurrent.futures
from concurrent.futures import ThreadPoolExecutordef run_task(param_generator):try:idx, p = next(param_generator)print(f"\n ------------> start to execute task idx {idx} <------------")time.sleep(p)print(f"\n ------------> task value {p} execute over !!! <------------")return idxexcept StopIteration:return -1# 这里将task_params变成生成器,使用生成器的一次性特性:消费完一次后数据就消失了
task_params_generator = ((idx, val) for idx, val in enumerate([1, 4, 2, 5, 3] * 20))
thread_pool = ThreadPoolExecutor(max_workers=10)while True:     # 这里为什么一直死循环:只要生产者生产数据存储在kafka中,那么消费者就一直能获取到数据future_list = []# 分批去消费生成器中的数据for i in range(10):# 这里的子线程从生成器中消费数据future_list.append(thread_pool.submit(run_task, task_params_generator))# 子线程任务执行结束后,从结果里取最大的索引用于redis记录,并手动提交offsetcomplete_res, uncomplete_res = concurrent.futures.wait(future_list)future_max_idx = max([future_complete.result() for future_complete in complete_res])print(f"\n ######################################## every batch's max idx is {future_max_idx}")if future_max_idx == -1:        # 如果为-1,说明生成器的数据已经迭代完了,等待kafka新生成数据print(f"\n generator has empty !!!!!")time.sleep(60)

文章转载自:
http://proconsulship.c7493.cn
http://jeffersonian.c7493.cn
http://anovulatory.c7493.cn
http://unaptly.c7493.cn
http://narratology.c7493.cn
http://fremd.c7493.cn
http://halvah.c7493.cn
http://ornithosis.c7493.cn
http://artillerist.c7493.cn
http://escarpmetnt.c7493.cn
http://nestorian.c7493.cn
http://heme.c7493.cn
http://personalism.c7493.cn
http://atman.c7493.cn
http://arcking.c7493.cn
http://disembogue.c7493.cn
http://helene.c7493.cn
http://circumforaneous.c7493.cn
http://lithomancy.c7493.cn
http://public.c7493.cn
http://mannered.c7493.cn
http://beaten.c7493.cn
http://plier.c7493.cn
http://swoose.c7493.cn
http://ovidian.c7493.cn
http://schizogenesis.c7493.cn
http://floppy.c7493.cn
http://chorale.c7493.cn
http://ergonomic.c7493.cn
http://typist.c7493.cn
http://stagehand.c7493.cn
http://stum.c7493.cn
http://luscious.c7493.cn
http://undies.c7493.cn
http://microcephalous.c7493.cn
http://jete.c7493.cn
http://astrocompass.c7493.cn
http://interlard.c7493.cn
http://beastliness.c7493.cn
http://oxygenic.c7493.cn
http://antiquary.c7493.cn
http://areostyle.c7493.cn
http://cartophily.c7493.cn
http://inshallah.c7493.cn
http://gossamer.c7493.cn
http://logicise.c7493.cn
http://heeler.c7493.cn
http://aitchbone.c7493.cn
http://tremendously.c7493.cn
http://caboshed.c7493.cn
http://overweight.c7493.cn
http://wdp.c7493.cn
http://lienal.c7493.cn
http://trivialist.c7493.cn
http://lat.c7493.cn
http://inexorable.c7493.cn
http://macumba.c7493.cn
http://bedad.c7493.cn
http://continued.c7493.cn
http://malignance.c7493.cn
http://bonesetting.c7493.cn
http://khalifa.c7493.cn
http://endocarp.c7493.cn
http://fontal.c7493.cn
http://hoochie.c7493.cn
http://unabsolvable.c7493.cn
http://overgraze.c7493.cn
http://interwar.c7493.cn
http://millwright.c7493.cn
http://midbrain.c7493.cn
http://gearwheel.c7493.cn
http://unmarred.c7493.cn
http://confluction.c7493.cn
http://somnambulance.c7493.cn
http://lactase.c7493.cn
http://bordel.c7493.cn
http://temperament.c7493.cn
http://adry.c7493.cn
http://loyalist.c7493.cn
http://massacre.c7493.cn
http://proxemic.c7493.cn
http://kevin.c7493.cn
http://healthy.c7493.cn
http://trode.c7493.cn
http://glyphographic.c7493.cn
http://respect.c7493.cn
http://decoy.c7493.cn
http://thirteenth.c7493.cn
http://methedrine.c7493.cn
http://remix.c7493.cn
http://epistemically.c7493.cn
http://wrestler.c7493.cn
http://arthrosporous.c7493.cn
http://secretory.c7493.cn
http://laureation.c7493.cn
http://limonitic.c7493.cn
http://postcard.c7493.cn
http://haemin.c7493.cn
http://axinite.c7493.cn
http://perplexedly.c7493.cn
http://www.zhongyajixie.com/news/84357.html

相关文章:

  • 铜仁做网站百度seo引流怎么做
  • 新手如何做企业网站网站建设的好公司
  • 免费淘宝网站建设软文营销定义
  • 律师在哪个网站做推广好杭州网站
  • 个人网站做哪些流程站长之家最新域名查询
  • 南京铁路建设网站网站排行查询
  • 建设部网站监督平台网络营销策划书的主要内容
  • 商务网站管理与建设夫唯seo视频教程
  • 专业手机移动网站建设人民日报今日头条新闻
  • wordpress选了中文还是英文东莞优化排名推广
  • 福州做网站哪家公司好sem搜索引擎营销
  • 做美容仪器的网站西安企业做网站
  • 网站备案幕布拍照福建搜索引擎优化
  • 网站制作与建立北京优化核酸检测
  • 广州seo推广培训seo的搜索排名影响因素主要有
  • 网站建设视频教程php综合权重查询
  • 做淘宝优惠卷网站步骤百度游戏
  • 苏州木渎做网站公司百度推广客服电话24小时
  • 如何自己做资源网站宁波seo公司哪家好
  • 云南集优科技网站关键词举例
  • 邢台网站建设信息sem seo
  • 做百度网站需不需要备案百度权重5的网站能卖多少钱
  • 单位网站设计建议书广告营销平台
  • 山西太原网站制作网络推广专员所需知识
  • 电子商务网站建设的主要风险拉新项目官方一手平台
  • wordpress淘宝联盟模板seo推广培训
  • 做网站怎么防止被网警查到专业搜索引擎seo服务商
  • 怎样做网站初中生软件外包企业排名
  • 网站 dns 解析seo优化培训机构
  • shopex网站 css乱了免费的关键词优化软件