我有一个由时间序列组成的数据帧。
日期索引|时间序列1|时间序列2|。。。等等
我使用pyRserve运行了一个使用R.的预测函数
我想使用芹菜来实现并行处理。我已经在以下上下文中编写了工作者代码。
def pipeR(k #input variable):
conn = pyRserve.connect(host = 'localhost', port = 6311)
# OPENING THE CONNECTION TO R
conn.r.i = k
# ASSIGNING THE PYTHON VARIABLE TO THAT OF IN THE R ENVIRONMENT
conn.voideval('''
WKR_Func <- forecst(a)
{
...# FORECASTS THE TIMESERIES IN COLUMN a OF THE DATAFRAME
}
''')
conn.eval('forecst(i)')
# CALLING THE FUNCTION IN R
group(pipeR.s(k) for k in [...list of column headers...])()
为了实现并行处理,我可以为所有工作进程都有一个单独的端口吗(就像我在上面的代码中所做的那样,端口:6311),或者我应该为不同的工作进程有不同的端口吗??
我当前收到一个错误
socketConnection中的错误("localhost",port=port,server=TRUE,blocking=TRUE,:无法打开连接
在R.中
当我为每个工作进程打开不同的端口时,问题得到了解决。。。
def pipeR( k, Frequency, Horizon, Split, wd_path):
# GENERATING A RANDOM PORT
port = randint(1000,9999)
# OPENING THE PORT IN THE R ENVIRONMENT
conn0 = pyRserve.connect(host = 'localhost', port = 6311)
conn0.r.port = port
conn0.voidEval
('''
library(Rserve)
Rserve(port = port, args = '--no-save')
''')
# OPENING THE PORT IN THE PYTHON ENVIRONMENT
conn = pyRserve.connect(host = 'localhost', port = port)
# ASSIGNING THE PYTHON VARIABLE TO THAT OF IN THE R ENVIRONMENT
conn.r.i = k
conn.voideval
('''
WKR_Func <- forecst(a)
{
...# FORECASTS THE TIMESERIES IN COLUMN a OF THE DATAFRAME
}
''')
conn.eval/('forecst(i)')
conn0.close()