2

我正在使用 fxcmpy 的 REST api 连接到我的 fxcmpy 帐户。自从升级到 1.2.6 版后,当我意外从服务器断开连接时,我遇到了重新连接问题。

我通过命令检测到断开连接

api.socket.on('disconnect',disconnect)

其中 disconnect 是我重新连接的回调函数:

def disconnect():
    FLAG=False
    while not FLAG:
    try :
        api=fxcmpy.fxcmpy(access_token=API_ACCESS_TOKEN,log_level='error',server='demo')
        api.subscribe_market_data(symbol,(automated_strategy,))
        FLAG=True
    except:
        print('be patient')
        time.sleep(60)
        FLAG=False

由于新版本我得到一个“服务器错误:无法连接到 FXCM 服务器”。或“数据包队列为空,正在中止”消息。

如果我重新启动我的 python 控制台,我可以重新启动我的脚本,直到下一次断开连接。我在 Windows 10、Raspbian 和 android 上试过这个:在所有情况下都是同样的问题。

我已经将 python-socketio 和 python-engineio 都更新到了它们的最新版本:没有变化。

当我遇到断开连接问题时,我正在寻找一种重新启动客户端的方法。有人有同样的问题/解决它的线索吗?

谢谢

4

3 回答 3

1

我花了一段时间,但我终于找到了解决方法。这个想法是完全重置 fxcmpy 库:将其删除,然后再次导入。

这是我的做法(代码仍未优化,您可以改进它,但想法就在这里):

while not FLAG:
    try :
        import sys
        a_del=[]
        for module in sys.modules.keys():
            if 'fxcm' in module:
                a_del.append(module)

        for module in a_del:
            del sys.modules[module]

        del fxcmpy

    except:
        print('error in reinitialization')
    try:
        del api
    except:
        print('could not delete api')

    try :
        import fxcmpy
        api=fxcmpy.fxcmpy(access_token=API_ACCESS_TOKEN,log_level='error',server='demo')
        api.subscribe_market_data(symbol,(automated_strategy,))

        FLAG=True
    except:
        print('try again')
        time.sleep(10)
        FLAG=False

这应该这样做(当然适应您的 api 对象名称和自动策略函数名称)。

于 2020-04-13T08:47:08.143 回答
0

除了重新安装 fxcmpy 模块,安装“python-socketio”对我有用

于 2020-05-11T15:36:47.507 回答
-1

这些天在处理同样的问题。我想问题是在打开新会话之前需要关闭会话。

就像是:

def disconnect():
global api


try:
    api.close()
except:
    pass

FLAG=False
while not FLAG:
    try :
        api=fxcmpy.fxcmpy(access_token=API_ACCESS_TOKEN,log_level='error',server='demo')
        api.subscribe_market_data(symbol,(automated_strategy,))
        FLAG=True
    except:
        print('be patient')
        time.sleep(60)
        FLAG=False

我很好奇您在需要时如何覆盖断开连接回调函数。例如在执行 KeyboardIntterupt 和 SystemExit 时?

于 2020-09-28T17:18:46.000 回答