• shutil concurrent.futures multiprocessing subprocess itertools 简介 区别


    简单并发

    shutil 高级文件操作

    concurrent.futures 多线程

    from concurrent.futures import ThreadPoolExecutor
    import shutil
    all_tsk = []
    with ThreadPoolExecutor(max_workers=4) as e:
        all_tsk.append( e.submit(shutil.copy, 'src1.txt', 'dest1.txt') )
        all_tsk.append( e.submit(shutil.copy, 'src2.txt', 'dest2.txt') )
        all_tsk.append( e.submit(shutil.copy, 'src3.txt', 'dest3.txt') )
        all_tsk.append( e.submit(shutil.copy, 'src4.txt', 'dest4.txt') )
        for future in all_tsk: # 还是串行!
            # do something to future.result()
        for future in as_completed(all_tsk): # 实现并行!
            # do something to future.result()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    TODO multiprocessing 多进程

    from multiprocessing import Process
    p = Process(target=tgt_fcn,args=tgt_fcn_args))
    p.start()
    p.join()
    
    • 1
    • 2
    • 3
    • 4

    TODO subprocess

    • subprocess.Popen
      • os.popen
    • subprocess.run subprocess.call
      • os.system

    itertools

    无穷迭代

    nameillustration
    count()count(10) → 10 11 12 13 14 …
    cycle()cycle(‘ABCD’) → A B C D A B C D …
    repeat()repeat(10, 3) → 10 10 10

    基于已有迭代

    nameillustration
    accumulate()accumulate([1,2,3,4,5]) → 1 3 6 10 15
    chain()chain(‘ABC’, ‘DEF’) → A B C D E F
    chain.from_iterable()chain.from_iterable([‘ABC’, ‘DEF’]) → A B C D E F
    compress()compress(‘ABCDEF’, [1,0,1,0,1,1]) → A C E F
    dropwhile()dropwhile(lambda x: x<5, [1,4,6,4,1]) → 6 4 1
    # starting when pred fails
    takewhile()takewhile(lambda x: x<5, [1,4,6,4,1]) → 1 4
    filterfalse()filterfalse(lambda x: x%2, range(10)) → 0 2 4 6 8
    groupby()[k for k, g in groupby(‘AAAABBBCCDAABBB’)] → A B C D A B
    [list(g) for k, g in groupby(‘AAAABBBCCD’)] → AAAA BBB CC D
    islice()islice(‘ABCDEFG’, 2, None) → C D E F G
    pairwise()pairwise(‘ABCDEFG’) → AB BC CD DE EF FG
    starmap()starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000
    tee()tee(‘ABC’,3) → [[A B C] [A B C] [A B C]]
    zip_longest()zip_longest(‘ABCD’, ‘xy’, fillvalue=‘-’) → Ax By C- D-

    排列组合

    nameillustration
    product()product(‘ABCD’, repeat=2) → AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
    permutations()permutations(‘ABCD’, 2) → AB AC AD BA BC BD CA CB CD DA DB DC
    combinations()combinations(‘ABCD’, 2) → AB AC AD BC BD CD
    combinations_with_replacement()combinations_with_replacement(‘ABCD’, 2) → AA AB AC AD BB BC BD CC CD DD

    TODO c++

    nameillustration
    accumulateAccumulate values in range (function template )
    adjacent_differenceCompute adjacent difference of range (function template )
    inner_productCompute cumulative inner product of range (function template )
    partial_sumCompute partial sums of range (function template )
    iotaStore increasing sequence (function template )
  • 相关阅读:
    MyQuartz高可用定时任务管理
    【MySQL】内置函数——字符串函数
    HttpMessageConverter 消息转换器
    Java函数式编程:二、高阶函数,闭包,函数组合以及柯里化
    IP网络广播景区广播广播系统
    Go HTTP 调用(下)
    LifeCycle 的使用和原理
    数据可视化训练第四天(模拟投掷筛子并且统计频次)
    rabbitMq应用问题
    Json文件序列化读取
  • 原文地址:https://blog.csdn.net/int_main_Roland/article/details/126099043