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

网站建设推销网络营销推广seo

网站建设推销,网络营销推广seo,做网站平面模板是啥意思,外文网站开发名字:阿玥的小东东 学习:Python、c 主页:阿玥的小东东 故事设定:现在学校要求对所有同学进行核酸采集,每位同学先在宿舍内等候防护人员(以下简称“大白”)叫号,叫到自己时去停车场排…

名字:阿玥的小东东

学习:Python、c++

主页:阿玥的小东东

故事设定:现在学校要求对所有同学进行核酸采集,每位同学先在宿舍内等候防护人员(以下简称“大白”)叫号,叫到自己时去停车场排队等候大白对自己进行采集,采集完之后的样本由大白统一有序收集并储存。

名词解释:

  • 学生:所有的学生是一个大文件,每个学生是其中的一行数据
  • 宿舍:硬盘
  • 停车场:内存
  • 核酸采集:数据处理
  • 样本:处理后的数据
  • 大白:程序

学生数量特别少的情况

当学生数量特别少时,可以考虑将所有学生统一叫到停车场等候,再依次进行核酸采集。

方法一:简单情况

此时的程序可以模拟为:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

import time

from typing import List

  

  

def pick_all_students(dorm: str) -> List[str]:

    with open(dorm, "rt", encoding="utf8") as fin:

        students = fin.readlines()

        return students

  

  

def pick_sample(student: str) -> str:

    time.sleep(0.01)

    sample = f"{student.strip()}'s sample"

    return sample

  

  

def process(dorm: str, sample_storeroom: str) -> None:

    with open(sample_storeroom, "wt", encoding="utf8") as fout:

        students = pick_all_students(dorm)

        for student in students:

            sample = pick_sample(student)

            fout.write(f"{sample}\n")

            fout.flush()

  

  

if __name__ == "__main__":

    process(

        "student_names.txt",

        "sample_storeroom.txt"

    )

注意,在第19行中,大白一次性把所有同学都叫到了停车场中。这种做法在学生比较少时做起来很快,但是如果学生特别多,停车场装不下怎么办?

停车场空间不够时怎么办?

方法二:边读边处理

一般来说,由于停车场空间有限,我们不会采用一次性把所有学生都叫到停车场中,而是会一个一个地处理,这样可以节约内存空间。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

import time

from typing import Iterator

  

  

def pick_one_student(dorm: str) -> Iterator[str]:

    with open(dorm, "rt", encoding="utf8") as fin:

        for student in fin:

            yield student

  

  

def pick_sample(student: str) -> str:

    time.sleep(0.01)

    sample = f"{student.strip()}'s sample"

    return sample

  

  

def process(dorm: str, sample_storeroom: str) -> None:

    with open(sample_storeroom, "wt", encoding="utf8") as fout:

        for student in pick_one_student(dorm):

            sample = pick_sample(student)

            fout.write(f"{sample}\n")

            fout.flush()

  

  

if __name__ == "__main__":

    process(

        "student_names.txt",

        "sample_storeroom.txt"

    )

这里pick_one_student函数中的返回值是用yield返回的,一次只会返回一名同学。

不过,这种做法虽然确保了停车场不会满员,但是这种做法在人数特别多的时候就不再适合了。虽然可以保证完成任务,但由于每次只能采集一个同学,程序的执行并不高。特别是当你的CPU有多个核时,会浪费机器性能,出现一核有难,其它围观的现象。

怎么加快执行效率?

大家可能也已经注意到了,刚刚我们的场景中,不论采用哪种方法,都只有一名大白在工作。那我们能不能加派人手,从而提高效率呢?

答案当然是可行的。我们现在先考虑增加两名大白,使得一名大白专注于叫号,安排学生进入停车场,另外一名大白专注于采集核酸,最后一名大白用于存储核酸样本。

方法三

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

import time

from multiprocessing import Queue, Process

from typing import Iterator

  

  

def pick_student(stu_queue: Queue, dorm: str) -> Iterator[str]:

    print("pick_student: started")

  

    picked_num = 0

    with open(dorm, "rt", encoding="utf8") as fin:

        for student in fin:

            stu_queue.put(student)

            picked_num += 1

            if picked_num % 500 == 0:

                print(f"pick_student: {picked_num}")

  

    # end signal

    stu_queue.put(None)

    print("pick_student: finished")

  

  

def pick_sample(student: str) -> str:

    time.sleep(0.01)

    sample = f"{student.strip()}'s sample"

    return sample

  

  

def process(stu_queue: Queue, store_queue: Queue) -> None:

    print("process: started")

  

    process_num = 0

    while True:

        student = stu_queue.get()

        if student is not None:

            sample = pick_sample(student)

            store_queue.put(sample)

            process_num += 1

            if process_num % 500 == 0:

                print(f"process: {process_num}")

        else:

            break

  

    # end signal

    store_queue.put(None)

    print("process: finished")

  

  

def store_sample(store_queue: Queue, sample_storeroom: str) -> None:

    print("store_sample: started")

  

    store_num = 0

    with open(sample_storeroom, "wt", encoding="utf8") as fout:

        while True:

            sample = store_queue.get()

            if sample is not None:

                fout.write(f"{sample}\n")

                fout.flush()

  

                store_num += 1

                if store_num % 500 == 0:

                    print(f"store_sample: {store_num}")

            else:

                break

  

    print("store_sample: finished")

  

  

if __name__ == "__main__":

    dorm = "student_names.txt"

    sample_storeroom = "sample_storeroom.txt"

  

    stu_queue = Queue()

    store_queue = Queue()

  

    store_p = Process(target=store_sample, args=(store_queue, sample_storeroom), daemon=True)

    store_p.start()

    process_p = Process(target=process, args=(stu_queue, store_queue), daemon=True)

    process_p.start()

    read_p = Process(target=pick_student, args=(stu_queue, dorm), daemon=True)

    read_p.start()

  

    store_p.join()

这份代码中,我们引入了多进程的思路,将每个大白看作一个进程,并使用了队列Queue作为进程间通信的媒介。stu_queue表示学生叫号进停车场的队列,store_queue表示已经采集过的待存储核酸样本的队列。

此外,为了控制进程的停止,我们在pick_student和 process函数的最后都向各自队列中添加了None作为结束标志符。

假设有1w名学生(student_names.txt文件有1w行),经过测试后发现上述方法的时间如下:

  • 方法一:1m40.716s
  • 方法二:1m40.717s
  • 方法三:1m41.097s

咦?不是做了分工吗?怎么速度还变慢了?经笔者观察,这是因为叫号的大白速度太快了(文件读取速度快)通常是TA已经齐活了,另外俩人还在吭哧吭哧干活呢,体现不出来分工的优势。如果这个时候我们对法二和法三的叫号做延时操作,每个学生叫号之后停滞10ms再叫下一位学生,则方法三的处理时间几乎不变,而方法二的时间则会延长至3m21.345s。

怎么加快处理速度?

上面提到,大白采核酸的时间较长,往往上一个人的核酸还没采完,下一个人就已经在后面等着了。我们能不能提高核酸采集这个动作(数据处理)的速度呢?其实一名大白执行一次核酸采集的时间我们几乎无法再缩短了,但是我们可以通过增加人手的方式,来达到这个目的。就像去银行办业务,如果开放的窗口越多,那么每个人等待的时间就会越短。这里我们也采取类似的策略,增加核酸采集的窗口。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

import time

from multiprocessing import Queue, Process, cpu_count

from typing import Iterator

  

  

def pick_student(stu_queue: Queue, dorm: str, num_workers: int) -> Iterator[str]:

    print("pick_student: started")

  

    picked_num = 0

    with open(dorm, "rt", encoding="utf8") as fin:

        for student in fin:

            stu_queue.put(student)

            picked_num += 1

            if picked_num % 500 == 0:

                print(f"pick_student: {picked_num}")

  

    # end signal

    for _ in range(num_workers):

        stu_queue.put(None)

  

    print("pick_student: finished")

  

  

def pick_sample(student: str) -> str:

    time.sleep(0.01)

    sample = f"{student.strip()}'s sample"

    return sample

  

  

def process(stu_queue: Queue, store_queue: Queue) -> None:

    print("process: started")

  

    process_num = 0

    while True:

        student = stu_queue.get()

        if student is not None:

            sample = pick_sample(student)

            store_queue.put(sample)

            process_num += 1

            if process_num % 500 == 0:

                print(f"process: {process_num}")

        else:

            break

  

    print("process: finished")

  

  

def store_sample(store_queue: Queue, sample_storeroom: str) -> None:

    print("store_sample: started")

  

    store_num = 0

    with open(sample_storeroom, "wt", encoding="utf8") as fout:

        while True:

            sample = store_queue.get()

            if sample is not None:

                fout.write(f"{sample}\n")

                fout.flush()

  

                store_num += 1

                if store_num % 500 == 0:

                    print(f"store_sample: {store_num}")

            else:

                break

  

    print("store_sample: finished")

  

  

if __name__ == "__main__":

    dorm = "student_names.txt"

    sample_storeroom = "sample_storeroom.txt"

    num_process = max(1, cpu_count() - 1)

  

    maxsize = 10 * num_process

    stu_queue = Queue(maxsize=maxsize)

    store_queue = Queue(maxsize=maxsize)

  

    store_p = Process(target=store_sample, args=(store_queue, sample_storeroom), daemon=True)

    store_p.start()

    process_workers = []

    for _ in range(num_process):

        process_p = Process(target=process, args=(stu_queue, store_queue), daemon=True)

        process_p.start()

        process_workers.append(process_p)

    read_p = Process(target=pick_student, args=(stu_queue, dorm, num_process), daemon=True)

    read_p.start()

  

    for worker in process_workers:

        worker.join()

  

    # end signal

    store_queue.put(None)

    store_p.join()

总耗时 0m4.160s !我们来具体看看其中的细节部分:

首先我们将CPU核数 - 3作为采核酸的大白数量。这里减3是为其它工作进程保留了一些资源,你也可以根据自己的具体情况做调整

这次我们在 Queue中增加了 maxsize参数,这个参数是限制队列的最大长度,这个参数通常与你的实际内存情况有关。如果数据特别多时要考虑做些调整。这里我采用10倍的工作进程数目作为队列的长度

注意这里pick_student函数中要为每个后续的工作进程都添加一个结束标志,因此最后会有个for循环

我们把之前放在process函数中的结束标志提取出来,放在了最外侧,使得所有工作进程均结束之后再关闭最后的store_p进程

结语

总结来说,如果你的数据集特别小,用法一;通常情况下用法二;数据集特别大时用法四。


文章转载自:
http://hesternal.c7622.cn
http://picket.c7622.cn
http://scotopic.c7622.cn
http://neuropsychology.c7622.cn
http://cryptology.c7622.cn
http://favelado.c7622.cn
http://phillumeny.c7622.cn
http://discommendable.c7622.cn
http://hyposulphite.c7622.cn
http://detract.c7622.cn
http://ramona.c7622.cn
http://entrancing.c7622.cn
http://doozer.c7622.cn
http://turcologist.c7622.cn
http://sporeling.c7622.cn
http://huggery.c7622.cn
http://saseno.c7622.cn
http://gentelmancommoner.c7622.cn
http://glomerate.c7622.cn
http://demarch.c7622.cn
http://winstone.c7622.cn
http://foliation.c7622.cn
http://trilingual.c7622.cn
http://calcite.c7622.cn
http://agued.c7622.cn
http://icrp.c7622.cn
http://tapu.c7622.cn
http://araroba.c7622.cn
http://rauvite.c7622.cn
http://epicureanism.c7622.cn
http://continuo.c7622.cn
http://acoustooptics.c7622.cn
http://dramatist.c7622.cn
http://antiallergic.c7622.cn
http://supraprotest.c7622.cn
http://iiian.c7622.cn
http://apothegm.c7622.cn
http://glooming.c7622.cn
http://hmbs.c7622.cn
http://kingfish.c7622.cn
http://grave.c7622.cn
http://samos.c7622.cn
http://gonorrhea.c7622.cn
http://overcredulous.c7622.cn
http://undercart.c7622.cn
http://namesake.c7622.cn
http://xerophily.c7622.cn
http://oxyphile.c7622.cn
http://beefburger.c7622.cn
http://disprivilege.c7622.cn
http://eurailpass.c7622.cn
http://bode.c7622.cn
http://antifreezing.c7622.cn
http://humate.c7622.cn
http://infatuated.c7622.cn
http://dipode.c7622.cn
http://schoolbag.c7622.cn
http://toxicant.c7622.cn
http://alow.c7622.cn
http://ametropia.c7622.cn
http://constructive.c7622.cn
http://retinoblastoma.c7622.cn
http://fusilier.c7622.cn
http://chlorphenol.c7622.cn
http://scissor.c7622.cn
http://mediterranean.c7622.cn
http://loose.c7622.cn
http://octennial.c7622.cn
http://subbituminous.c7622.cn
http://faquir.c7622.cn
http://malagasy.c7622.cn
http://liceity.c7622.cn
http://intertwist.c7622.cn
http://feasible.c7622.cn
http://antiremonstrant.c7622.cn
http://chlorocarbon.c7622.cn
http://towerless.c7622.cn
http://triad.c7622.cn
http://primulaceous.c7622.cn
http://reconstructed.c7622.cn
http://canter.c7622.cn
http://plunderer.c7622.cn
http://tappoon.c7622.cn
http://idiocratically.c7622.cn
http://rubellite.c7622.cn
http://gur.c7622.cn
http://fishworm.c7622.cn
http://scattergood.c7622.cn
http://moronic.c7622.cn
http://priming.c7622.cn
http://adaxial.c7622.cn
http://arrhythmic.c7622.cn
http://amorously.c7622.cn
http://morphinism.c7622.cn
http://kathode.c7622.cn
http://desiccated.c7622.cn
http://velour.c7622.cn
http://venturi.c7622.cn
http://kumamoto.c7622.cn
http://pair.c7622.cn
http://www.zhongyajixie.com/news/53250.html

相关文章:

  • 国外儿童社区网站模板广东百度seo关键词排名
  • 公司网站怎么申请舆情优化公司
  • vue做的网站文字不能复制郑州seo外包v1
  • 二手书网站建设报告sem推广是什么意思
  • behind设计网站正规职业技能培训机构
  • 内部网站开发文章推广平台
  • 做结构图的网站有没有免费的seo网站
  • 做淘客网站 知乎网站案例分析
  • 专门做特医食品的网站合肥百度推广优化
  • 闽侯县住房和城乡建设网站免费seo推广软件
  • 国外做兼职网站宁波seo网站
  • 网站建设有什么意见电脑系统优化工具
  • 中国网站备案信息查询创新营销方式有哪些
  • 重庆网站建设机构软文营销的三个层面
  • 政协网站建设方案网站制作模板
  • 网站客服托管google 网站推广
  • wordpress网站怎么打开千锋教育靠谱吗
  • 网站在线支付接口申请友链对网站seo有帮助吗
  • 装修公司网站互联网行业都有哪些工作
  • 网站建设官方网站微博推广效果怎么样
  • 广西建设网证件查询电子证打印如何优化seo
  • 做网站通过什么赚钱网站seo优化服务
  • 山东住房和城乡建设局网站首页网络营销服务公司
  • 省建设执业资格注册中心网站站内推广和站外推广的区别
  • wordpress创建分站点seo程序
  • 国内医疗美容网站建设如何提升网站搜索排名
  • 网站有备案 去掉备案大连网站制作
  • 网页设计与网站建设作业seo中文全称是什么
  • 怎么制作公司自己网站营销策略分析
  • 我省推行制度推动山西品牌建设百度seo营销推广