如何得到收益率指标?

2024-05-23 22:08:41 发布

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

我有这样的代码:

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的索引呢?你知道吗


Tags: indexschemadefcpfilecommitfuncprint
1条回答
网友
1楼 · 发布于 2024-05-23 22:08:41

我可能不完全理解您要做的事情,但是下面不使用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))

相关问题 更多 >