【问题标题】:Connection issues with fxcmpy REST apifxcmpy REST api 的连接问题
【发布时间】:2020-02-21 18:11:08
【问题描述】:

我正在使用 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 更新到最新版本:没有变化。

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

谢谢

【问题讨论】:

  • 您好,我也有同样的问题,您找到解决方案了吗?

标签: python forex python-socketio


【解决方案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 对象名称和自动策略函数名称)。

【讨论】:

  • 实际上,我错误地打开了两个连接,之后我的连接被中止了。尝试运行您的代码,但我仍然会中止连接。我该怎么办?
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    类似:

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

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2020-08-17
      • 2014-01-14
      相关资源
      最近更新 更多