Python:如何在Python中运行嵌套并行进程?

rol*_*and 5 python parallel-processing python-multiprocessing

我有一个df交易者交易数据集。我有 2 个级别的 for 循环,如下所示:

smartTrader =[]

for asset in range(len(Assets)):
    df = df[df['Assets'] == asset]
    # I have some more calculations here
    for trader in range(len(df['TraderID'])):
        # I have some calculations here, If trader is successful, I add his ID  
        # to the list as follows
        smartTrader.append(df['TraderID'][trader])

    # some more calculations here which are related to the first for loop.
Run Code Online (Sandbox Code Playgroud)

我想并行化 中每个资产的计算Assets,并且我还想并行化每个资产的每个交易者的计算。完成所有这些计算后,我想根据smartTrader.

这是我第一次尝试并行处理,所以请耐心等待,非常感谢您的帮助。

Fel*_*ema 0

不要使用for,而是使用map

import functools
smartTrader =[]

m=map( calculations_as_a_function, 
        [df[df['Assets'] == asset] \
                for asset in range(len(Assets))])
functools.reduce(smartTradder.append, m)
Run Code Online (Sandbox Code Playgroud)

从那时起,您可以尝试不同的并行map实现multiprocessingsastackless'