我的问题似乎类似于This Thread但是,虽然我认为我正在遵循建议的方法,但我仍然收到 PicklingError。当我在本地运行我的进程而不发送到 IPython Cluster Engine 时,该函数工作正常。
我在 IPyhon 的 notebook 上使用 zipline,所以我首先创建了一个基于 zipline.TradingAlgorithm 的类
细胞 [ 1 ]
from IPython.parallel import Client
rc = Client()
lview = rc.load_balanced_view()
细胞 [ 2 ]
%%px --local # This insures that the Class and modules exist on each engine
import zipline as zpl
import numpy as np
class Agent(zpl.TradingAlgorithm): # must define initialize and handle_data methods
def initialize(self):
self.valueHistory = None
pass
def handle_data(self, data):
for security in data.keys():
## Just randomly buy/sell/hold for each security
coinflip = np.random.random()
if coinflip < .25:
self.order(security,100)
elif coinflip > .75:
self.order(security,-100)
pass
细胞 [ 3 ]
from zipline.utils.factory import load_from_yahoo
start = '2013-04-01'
end = '2013-06-01'
sidList = ['SPY','GOOG']
data = load_from_yahoo(stocks=sidList,start=start,end=end)
agentList = []
for i in range(3):
agentList.append(Agent())
def testSystem(agent,data):
results = agent.run(data) #-- This is how the zipline based class is executed
#-- next I'm just storing the final value of the test so I can plot later
agent.valueHistory.append(results['portfolio_value'][len(results['portfolio_value'])-1])
return agent
for i in range(10):
tasks = []
for agent in agentList:
#agent = testSystem(agent,data) ## On its own, this works!
#-- To Test, uncomment the above line and comment out the next two
tasks.append(lview.apply_async(testSystem,agent,data))
agentList = [ar.get() for ar in tasks]
for agent in agentList:
plot(agent.valueHistory)
这是产生的错误:
PicklingError Traceback (most recent call last)/Library/Python/2.7/site-packages/IPython/kernel/zmq/serialize.pyc in serialize_object(obj, buffer_threshold, item_threshold)
100 buffers.extend(_extract_buffers(cobj, buffer_threshold))
101
--> 102 buffers.insert(0, pickle.dumps(cobj,-1))
103 return buffers
104
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
如果我使用以下内容覆盖 zipline.TradingAlgorithm 中的 run() 方法:
def run(self, data):
return 1
尝试这样的事情...
def run(self, data):
return zpl.TradingAlgorithm.run(self,data)
导致相同的 PicklingError。
然后传递给引擎的工作,但显然没有执行测试的胆量。由于 run 是 zipline.TradingAlgorithm 内部的一种方法,我不知道它所做的一切,我将如何确保它通过?