• Linux环境下省时省力的线程池代码分享


    Linux环境下省时省力的线程池代码分享

    前言和code注释

    Linux的环境下,当我们要跑多个线程的时候,需要一个个跑,费时费力。每次出现问题重新跑的时候,就要在输入一次次重复的启动线程的命令行,这显然很麻烦。所以这边分享到一个线程池的代码,可以根据自己的需要在对应部分进行修改,然后运行代码

    import multiprocessing
    import os
    from concurrent.futures import ThreadPoolExecutor
    
    • 1
    • 2
    • 3

    引入一些代码的模块,这样可以调用相应的库函数

    def inst0():
    		os.system('./LKH bbz25234-ALPHA.par ')
    
    • 1
    • 2

    定义一个线程里面需要运行的代码,并封装在一个function中

    
    pool = ThreadPoolExecutor(max_workers=23)
    
    • 1
    • 2

    定义线程池pool和线程数为23

    pool.submit(inst0)
    
    • 1

    加入进程

    pool.shutdown()
    
    • 1

    在所有线程跑完之后,关闭线程池
    下面请看具体的完整代码:

    代码

    import multiprocessing
    import os
    from concurrent.futures import ThreadPoolExecutor
    
    def inst0():
    		os.system('./LKH bbz25234-ALPHA.par ')
    
    def inst1():
    		os.system('./LKH boa28924-ALPHA.par ')
    
    def inst2():
    		os.system('./LKH fma21553-ALPHA.par ')
    
    def inst3():
    		os.system('./LKH fnc19402-ALPHA.par ')
    
    def inst4():
    		os.system('./LKH frh19289-ALPHA.par ')
    
    def inst5():
    		os.system('./LKH fyg28534-ALPHA.par ')
    
    def inst6():
    		os.system('./LKH icx28698-ALPHA.par ')
    
    def inst7():
    		os.system('./LKH ido21215-ALPHA.par ')
    
    def inst8():
    		os.system('./LKH ird29514-ALPHA.par ')
    
    def inst9():
    		os.system('./LKH irx28268-ALPHA.par ')
    
    def inst10():
    		os.system('./LKH lsb22777-ALPHA.par ')
    
    def inst11():
    		os.system('./LKH pjh17845-ALPHA.par ')
    
    def inst12():
    		os.system('./LKH xia16928-ALPHA.par ')
    
    def inst13():
    		os.system('./LKH xmc10150-ALPHA.par ')
    
    def inst14():
    		os.system('./LKH xrb14233-ALPHA.par ')
    
    def inst15():
    		os.system('./LKH xrh24104-ALPHA.par ')
    
    def inst16():
    		os.system('./LKH xvb13584-ALPHA.par ')
    
    def inst17():
    		os.system('./LKH fi10639-ALPHA.par ')
    
    def inst18():
    		os.system('./LKH ho14473-ALPHA.par ')
    
    def inst19():
    		os.system('./LKH it16862-ALPHA.par ')
    
    def inst20():
    		os.system('./LKH mo14185-ALPHA.par ')
    
    def inst21():
    		os.system('./LKH sw24978-ALPHA.par ')
    
    def inst22():
    		os.system('./LKH vm22775-ALPHA.par ')
    
    
    
    pool = ThreadPoolExecutor(max_workers=23)
    
    pool.submit(inst0)
    pool.submit(inst1)
    # pool.submit(inst2)
    pool.submit(inst3)
    pool.submit(inst4)
    pool.submit(inst5)
    pool.submit(inst6)
    pool.submit(inst7)
    pool.submit(inst8)
    pool.submit(inst9)
    pool.submit(inst10)
    pool.submit(inst11)
    pool.submit(inst12)
    pool.submit(inst13)
    pool.submit(inst14)
    pool.submit(inst15)
    pool.submit(inst16)
    pool.submit(inst17)
    pool.submit(inst18)
    pool.submit(inst19)
    pool.submit(inst20)
    pool.submit(inst21)
    pool.submit(inst22)
    pool.shutdown()
    
    
    • 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
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
  • 相关阅读:
    数字孪生3d车间、虚拟车间、数字化三维车间的案例比较
    abc324 e
    【LeetCode热题100】--56.合并区间
    责任链模式auto-pipeline工具使用及源码解析
    C语言高级教程-C语言数组(三)
    从零开始学React--JSX
    STM32--ESP8266物联网WIFI模块(贝壳物联)--远程无线控制点灯
    mysql 事务的实用小实例
    AI绘画:StableDiffusion实操教程-斗破苍穹-云韵-常服(附高清图下载)
    flutter plugins插件【一】【FlutterJsonBeanFactory】
  • 原文地址:https://blog.csdn.net/wlfyok/article/details/134335359