如何将字典传递给并发期货执行器

2024-09-27 02:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我对使用并发期货还不熟悉,我找不到任何关于如何做到这一点的例子。我有一个全局字典data,我希望并发未来执行器调用的函数将结果添加到其中。函数可以工作,但数据中没有输出。 谢谢你的帮助, T

def estimate_shannon_entropy(dna_sequence):
    bases = collections.Counter([tmp_base for tmp_base in dna_sequence])
    # define distribution
    dist = [x/sum(bases.values()) for x in bases.values()]

    # use scipy to calculate entropy
    entropy_value = entropy(dist, base=2)
    #norm_ent = entropy_value/math.log(len(dna_sequence),2)
    return entropy_value

def shan(i):
    
    name1=i.split("/")[-1]
    
    ext1=name1.split(".")[-1]
    
    print(name1)
    
    if ext1=="gz":
        #print("gz detected")
        f=gzip.open(i,'rt')
        k=name1.split(".")[-2]
    
    else:
        f=open(i,'r')
        k=ext
    
    if k[-1]=="a":
        fmt="fasta"
        #print("fasta")
    if k[-1]=="q":
        fmt="fastq"
        #print("fastq")
    c=0
    shannon_total=0
    for x in SeqIO.parse(f,fmt):
        c=c+1
        if c<=samples:
            shannon = estimate_shannon_entropy(str(x.seq))
            shannon_total = shannon_total +shannon
        
    ans=float(shannon_total/samples)
    
    data[name1]=ans
    
folder=sys.argv[1] 
filelist=glob.glob(folder)
filelist.sort(key=tokenize)
#print(filelist)

samples=int(sys.argv[2])
threads=int(sys.argv[3])

global data
data={}

executor = concurrent.futures.ProcessPoolExecutor(threads)
futures = [executor.submit(shan, i) for i in filelist]
concurrent.futures.wait(futures)

print(data)

Tags: infordatabaseifdnatotalentropy
1条回答
网友
1楼 · 发布于 2024-09-27 02:24:43

好的,我找到了答案,如果有更好的方法(当然有),我将离开这里。 旧经理:

from multiprocessing import Manager
manager=Manager()
data=manager.dict()
executor = concurrent.futures.ProcessPoolExecutor(threads)
futures = [executor.submit(shan, i,data) for i in filelist]
concurrent.futures.wait(futures)

相关问题 更多 >

    热门问题