fxcmpy REST 的连接问题 api

Connection issues with fxcmpy REST api

我正在使用 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

自从新版本以来,我收到 "ServerError: Can not connect to FXCM Server." 或 "packet queue is empty, Aborting" 消息。

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

我已将 python-socketio 和 python-engineio 更新到最新版本:没有变化。

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

谢谢

我花了一段时间,但我终于找到了解决方法。这个想法是完全重置 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 对象名称和自动策略函数名称)。

除了重新安装 fxcmpy 模块外,安装 "python-socketio" 对我也很有效

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

类似于:

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 时?