我有一个以下问题。我试图重构我的代码,以便使用多线程处理API调用。我的核心数据是以下格式的元组的简单列表:Python的pool.map列表元组
lst = [('/Users/sth/photo1.jpg',
'/Users/sth/photo2'),
('/Users/sth/photo1.jpg',
'/Users/sth/photo3'), (...)]
功能,我使用需要LST列表,并对其进行处理,通过这需要对照片的API。毕竟每一对都返回一个数字。到目前为止,我正在使用一个循环将一个元组放入我的函数中,并生成所提到的数字。我想以一种方式占用我列表的一部分并调用批处理内的元组的函数的方式将整个计算对齐。要做到这一点,我试图用池功能多模块:
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(2)
results = pool.map(score_function, lst)
然而,出现以下错误:
IOError: [Errno 2] No such file or directory: 'U'
奇怪的事情发生在这里。它试图将我元组中的单个字符作为参数。任何想法如何正确地做到这一点?
谢谢
@edit
缺乏score_function定义的是我不好。让我更新的问题:
def score_function(pairs):
score_list = list()
for pair in pairs:
score = findElement(target = pair[0], source = pair[1])
score_list.append([pair[0], pair[1], score])
return score_list
凡findElement被定义为:
def findElement(target, source):
with open(source, 'rb') as source_:
source_bytes = source_.read()
with open(target, 'rb') as target_:
target_bytes = target_.read()
score = API_request(target_bytes = target_bytes,
source_bytes = source_bytes)
return score
您是否也可以分享您的score_function,至少关键部分如定义以及如何访问该参数。 – Hannu
@Hannu你说得对。我刚刚添加了score_function定义 –
我发布了另一个答案,它应该有所帮助。 – Hannu