从返回的pool.apply_asyn中订购数据

2024-10-02 10:26:59 发布

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

我正在写一个隐写程序。我现在有我想要的大部分工作。然而,我希望使用多个进程重建消息,这显然意味着需要对从进程返回的位进行排序。所以目前我有:


好的,我回家了,我要放一些实际的代码。在

def message_unhide(data):
    inp = cv.LoadImage(data[0]) #data[0] path to image
    steg = LSBSteg(inp)
    bin = steg.unhideBin()
    return bin

#code in main program underneath
count = 0
f = open(files[2], "wb") #files[2] = name of file to rebuild
fat = open("fat.txt", 'w+')
inp = cv.LoadImage(files[0][count]) # files[0] directory path of images
steg = LSBSteg(inp)
bin = steg.unhideBin()
fat.write(bin)
fat.close()
fat = open("fat.txt", 'rb')
num_files = fat.read() #amount of images message hidden across
fat.close()
count += 1
pool = Pool(5)
binary = []
''' Just something I was testing
for x in range(int(num_files)):
        binary.append(0)
    print (binary)
'''
while count <= int(num_files):
        data = [files[0][count], count]
        #f.write(pool.apply(message_unhide, args=(data, )))                #
        #binary[count - 1] = [pool.apply_async(message_unhide, (data, ))]  #
        #again just another few ways i was trying to overcome
        binary = [pool.apply_async(message_unhide, (data, ))]
        count += 1
pool.close()
pool.join()
bits = [b.get() for b in binary]
print(binary)

#for b in bits:
    #    f.write(b)
f.close()

此方法只是重写二进制文件

^{pr2}$

这个方法填充了整个二进制文件,但是我丢失了.get()

^{3}$

很抱歉,我的编码太草率了,我当然不是专家。在


Tags: toinmessageclosedatabincountfiles
1条回答
网友
1楼 · 发布于 2024-10-02 10:26:59

您的主要问题是在循环中重写binary。列表中只有一个项目,因为您要丢弃以前的列表,每次都重新创建它。相反,您应该使用append来修改现有列表:

binary.append(pool.apply_async(message_unhide, (data, )))

但是如果您使用pool.map而不是滚动自己的版本,可能会有更好的时间。它期望在每次迭代时将产生单个参数的iterable传递给函数,并返回返回值的列表。map调用会一直阻塞,直到所有值都准备好,因此您不需要任何其他同步逻辑。在

下面是一个使用生成器表达式动态构建data参数项的实现。如果重写message_unhide以直接接受文件名作为其参数,而无需为列表编制索引(似乎从未使用索引),则可以将files[0]传递到{}中:

^{pr2}$

相关问题 更多 >

    热门问题