python多任务,线程池

25个线程要做完1000个任务

import threading,time,random,os,threadpool
from shutil import copyfile

def dealing(cata):
    print threading.current_thread().name
    print 'entering ',cata
    os.popen('sh DORFTN '+cata).read()
#    os.chdir('..')

if __name__ == "__main__":
    cata=[c for c in os.listdir('.') if os.path.isdir(c)]
   pool=threadpool.ThreadPool(25)
   requests=threadpool.makeRequests(dealing,cata)
    [pool.putRequest(req) for req in requests]
    pool.wait()


如果函数内有多个参数,可如下处理:
import threadpool, os, glob

out=[]

def processing(sample,trace):
   tmp=sample.split('/')
   tracefile=trace+'/'+tmp[-1]
   ret=os.popen('./doublet_search %s %s' % (sample,tracefile)).read()
    print(ret)

if __name__=='__main__':
   traces=glob.glob('Trace/2*')
   samples=glob.glob('Template/2*')
    arglist=[]
    for sample in samples:
       Zs=glob.glob(sample+'/*.?HZ')
       for Z in Zs:
          sam=Z[0:-1]
           for trace in traces:
             arglist.append(([sam,trace],None))
#    print(arglist)
   pool_t=threadpool.ThreadPool(8)
   requests=threadpool.makeRequests(processing,arglist)
    [pool_t.putRequest(req) for req in requests]
    pool_t.wait()

****注:一般在函数中不要用到os.chdir,因为chdir是全局的,如果要用,则要用绝对路径。

如何把包含中文字体的matplotlib图件保存为pdf文件

import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages

mpl.rcParams['pdf.fonttype']=42
mpl.rcParams['font.family']='WenQuanYi Zen Hei'

with PdfPages('test.pdf') as pdf:
xxxx # plot some figure
pdf.savefig()

#questions/45870858/runtimeerror-failed-to-open-truetype-font-in-matplotlib-backends-pdf-in-python stackoverflow

如何获取ndarray中索引以外的其它所有值

方法一:

import numpy as np
a = np.array([0,1,2,3,4,5,5,6,7,8,9])
print(a)
print(a.sum())
# [0 1 2 3 4 5 5 6 7 8 9]
# 50
a = np.ma.array(a, mask=False)
a.mask[3] = True
print(a)
print(a.sum())
# [0 1 2 -- 4 5 5 6 7 8 9]
# 47
方法二
a_new = np.delete(a, 3, 0)
这个3可以是个索引列表

C字符串

想把一列台站名输出成一行,用空格隔开,写了个C。

fgets(buff,1024,fp);
strncpy(temp,buff,strlen(buff)-1);
printf('%s ',temp);

发现当台站名由长变短时,会“继承”上一个台站的后面几个字符,比如前一个台站名为L5317,而后一个为Y02时,前一个正确,而后一个会变成Y0217.怀疑是temp保存了上一次的’\0’的位置,所以在每次printf后强行temp[0]=’\0’,发现没用,考虑是上次的字符串终止符还在。
我试了以下几种方法,都行:

1 每次printf后,temp[strlen(a)-1]=’0’;

2 用malloc给temp分配内存,读到了新的,就realloc一次,记得最后free掉;   temp=(char *)realloc(temp,sizeof(char)*(strlen(buff)-1))

3 直接把buff数组的最后一个值换成’\0’,然后输出buff。 buff[strlen(buff)-1]=’\0′