我有以下代码:
@classmethod
def load(self):
with open('yaml/instruments.yaml', 'r') as ymlfile:
return {v['name']: Instrument(**v) for (v) in list(yaml.load_all(ymlfile))}
我想用如下方式并行加载这些文件:
return ThreadPoolExecutor.map(Instrument, list(yaml.load_all(ymlfile))
但我不太清楚如何让参数通过。你知道吗
下面是一个仪器.yaml地址:
---
name: 'corn'
#Trade December corn only
multiplier: 5000
contract_prefix: 'C'
months_traded: [3, 5, 7, 9, 12]
quandl: 'CHRIS/CME_C2'
first_contract: 196003
backtest_from: 199312
trade_only: [12]
contract_name_prefix: 'C'
quandl_database: 'CME'
slippage: 0.125 #half the spread
ib_code: 'ZC'
如何将代码重构为映射,以便使用ThreadPoolExecutor?你知道吗
简单的解决方案是定义一个顶级的简单辅助函数,以便在executor中使用:
然后更改:
收件人:
正如我在评论中指出的,这可能不会给您带来任何好处;the GIL意味着线程不会提高性能,除非:
除非
Instrument
的构造成本非常高,否则即使使用ProcessPoolExecutor
也不会有帮助;您需要在分派的任务中做大量有意义的工作,或者您在任务管理(以及进程、序列化和进程间通信)上的时间比并行性上的时间要多。你知道吗相关问题 更多 >
编程相关推荐