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()
TODO multiprocessing 多进程
from multiprocessing import Process
p = Process(target=tgt_fcn,args=tgt_fcn_args))
p.start()
p.join()
TODO subprocess
| name | illustration |
|---|---|
| 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 |
| name | illustration |
|---|---|
| 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- |
| name | illustration |
|---|---|
| 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++
| name | illustration |
|---|---|
| accumulate | Accumulate values in range (function template ) |
| adjacent_difference | Compute adjacent difference of range (function template ) |
| inner_product | Compute cumulative inner product of range (function template ) |
| partial_sum | Compute partial sums of range (function template ) |
| iota | Store increasing sequence (function template ) |