我有这样的代码:
def iter_commit(LogFileName):
with open(path.join(__folder__, LogFileName)) as LogFileName_file:
for index, schema in enumerate(LogFileName_file):
if len(schema) > 10:
yield index, schema.rstrip('\n')
def makePool(cP, func, iters):
try:
pool = ThreadPool(cP)
pool.map_async(func,iters).get(99999)
pool.close()
pool.join()
except:
print('Pool Error')
raise
finally:
pool.terminate()
def Parse(RoWnum, Link):
print(RoWnum, Link)
makePool(50, partial(Parse, iter_commit(strSiteMap)[0]),
iter_commit(strSiteMap)[1])
但我发现错误TypeError: 'generator' object is not subscriptable
那么如何获得yield
的索引?
答案 0 :(得分:1)
我可能不完全理解你要做的是什么,但也许以下(不使用functools.partial()
)可能会有所帮助,因为它可以在不需要索引值的情况下工作。它的作用是将makePool()
函数传递给迭代器,迭代器将生成Parse()
函数所期望的配对值,并将它们作为元组传递。
from multiprocessing.pool import ThreadPool
def pairwise(iterable):
"s -> (s0,s1), (s2,s3), (s4, s5), ..."
a = iter(iterable)
return zip(a, a)
strSiteMap = ['site0', 13, 'site1', 42]
def makePool(cP, func, iters):
try:
pool = ThreadPool(cP)
pool.map_async(func, iters).get(99999)
pool.close()
pool.join()
except Exception:
print('Pool Error')
raise
finally:
pool.terminate()
def Parse(args):
RowNum, Link = args
print('Parse({!r}, {!r}) called'.format(RowNum, Link))
makePool(50, Parse, pairwise(strSiteMap))