网络连接丢失并重新建立后,无法重新连接到KDB服务器

时间:2020-03-15 04:28:52

标签: python pandas connection kdb qpython

环境:Python 3.8.1,Windows 10 x64,pandas,qpython。

简介

我有一个脚本,该脚本使用VPN连接连接到远程KDB服务器。 (KDB服务器仅允许串行请求(没有并行请求))。问题是我的ISP每隔几个小时断开一次连接,然后ISP还原连接,但是分配了不同的IP。当连接断开时,安装在我的开发计算机上的VPN客户端也会断开连接。然后,我重新建立了VPN连接,但是脚本无法继续工作,只有重新启动整个脚本才能使其继续工作。

这是代码的一部分,负责重新建立连接: 不幸的是,它不像我想象的那样起作用。

代码说明:

我已将try / except语句放入循环中。一切正常(发送查询等),直到命中为止。

我怀疑内部连接丢失,因此我再次放入q.open()。根据输出,应该重新建立连接,但是不是。由于某种原因,它陷入了无限循环,我无法弄清原因。

            sendQueryOpenPricesGood = False
            while not sendQueryOpenPricesGood:
                try:  # sending the query
                    print('\nattempt to send open prices query to KDB server (quick, several seconds)...') # debug
                    df_openPrices = pd.DataFrame(q.sendSync(query_openPrices))
                    sendQueryOpenPricesGood = True
                    print('Open prices query sent!\n') # debug
                except:  # if anythong wrong happens, retry
                    print('Connection error occurred on sending open prices query to KDB server (keyword: sendQueryOpenPricesGood). Is VPN turned on?')

                    print('\nattempt to close the connection to KDB server. Loop: sendQueryOpenPricesGood')
                    q.close()
                    print('connection to KDB server closed!.')

                    print('attempt to re-establish the connection to KDB server...')
                    q.open()
                    print('Connection to KDB server re-established!\n')
                    continue

输出:

attempt to send open prices query to KDB server (quick, several seconds)...
Connection error occurred on sending open prices query to KDB server (keyword: sendQueryOpenPricesGood). Is VPN turned on?
attempt to re-establish the connection to KDB server...
Connection to KDB server re-established!

尝试/除外/继续的方法是valid,但是显然我缺少了一些东西。

编辑:

我添加了一条语句,首先关闭 q.close(),然后尝试使用 q.open()建立新的连接。

平均时间一滴互联网连接发生了。似乎关闭连接首先有帮助。但是,还需要一些时间来确保它确实有效。

0 个答案:

没有答案