文件为python的流API IG Markets的Linux NoHup失败

tquggr8v  于 2023-03-01  发布在  Linux
关注(0)|答案(1)|浏览(93)

这是一个关于nohup在linux中运行python文件的非常具体的问题。背景故事,我试图保存流数据(来自IG markets广播信号)。而且,当我试图通过远程服务器运行它时(这样我就不必让我自己的本地桌面24/7运行),不知何故,当nohup收听广播信号时,它不会参与。
下面是python代码示例

#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
IG Markets Stream API sample with Python
    """

user_ = 'xxx'
password_ = 'xxx'
api_key_ = 'xxx' # this is the 1st api key
account_ = 'xxx'
acc_type_ = 'xxx'
fileLoc = 'marketdata_IG_spx_5min.csv'

list_ = ["CHART:IX.D.SPTRD.DAILY.IP:5MINUTE"]
fields_ = ["UTM", "LTV", "TTV", "BID_OPEN",  "BID_HIGH",  \
           "BID_LOW", "BID_CLOSE",]

import time
import sys
import traceback
import logging

import warnings
warnings.filterwarnings('ignore')

from trading_ig import (IGService, IGStreamService)
from trading_ig.lightstreamer import Subscription

cols_ = ['timestamp', 'data']
# A simple function acting as a Subscription listener
def on_prices_update(item_update):
    # print("price: %s " % item_update)
    print("xxxxxxxx
          ))
    

# A simple function acting as a Subscription listener
def on_charts_update(item_update):
    # print("price: %s " % item_update)
    print(xxxxxx"\
          .format(
              stock_name=item_update["name"], **item_update["values"]
          ))
    res_ = [xxxxx"\
            .format(
              stock_name=item_update["name"], **item_update["values"]
          ).split(' '))]
    # display(pd.DataFrame(res_))
    
    try:
        data_ = pd.read_csv(fileLoc)[cols_]
        data_ = data_.append(pd.DataFrame(res_, columns = cols_))
        
        data_.to_csv(fileLoc)
        print('there is data and we are reading it')
        # display(data_)
    except:
        pd.DataFrame(res_, columns = cols_).to_csv(fileLoc)
        print('there is no data and we are saving first time')
        
    time.sleep(60) # sleep for 1 min

def main():
    logging.basicConfig(level=logging.INFO)
    # logging.basicConfig(level=logging.DEBUG)

    ig_service = IGService(
        user_, password_, api_key_, acc_type_
    )

    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    accountId = account_
    
    
    ################ my code to set sleep function to sleep/read at only certain time intervals
    s_time = time.time()
    ############################
    
    # Making a new Subscription in MERGE mode
    subscription_prices = Subscription(
        mode="MERGE",
        # make sure to put L1 in front of the instrument name
        items= list_,
        fields= fields_
    )
    # adapter="QUOTE_ADAPTER")

    # Adding the "on_price_update" function to Subscription
    subscription_prices.addlistener(on_charts_update)

    # Registering the Subscription
    sub_key_prices = ig_stream_service.ls_client.subscribe(subscription_prices)
    print('this is the line here')
    
    input("{0:-^80}\n".format("HIT CR TO UNSUBSCRIBE AND DISCONNECT FROM \
    LIGHTSTREAMER"))

    # Disconnecting
    ig_stream_service.disconnect()

if __name__ == '__main__':
    main()

#######

然后,我尝试在Linux上使用以下命令运行它:nohup python marketdata.py其中marketdata.py基本上是上面的python代码。
不知何故,nohup不会参与.......任何Maven/大师谁可能会看到我在我的代码中遗漏了什么?

tvz2xvvm

tvz2xvvm1#

修复的方法是不让代码通过main()运行。2相反,行命令应该直接在shell脚本中。
因此,如果我们展开下面的代码,它就可以在使用nohup的基于云的远程服务器中工作,而不仅仅是main()。

if __name__ == '__main__':
    
    logging.basicConfig(level=logging.INFO)
    
    ig_service = IGService(user_, password_, api_key_, acc_type_)
    ig_stream_service = IGStreamService(ig_service)
    ig_session = ig_stream_service.create_session()
    
    # create a subscription and add listeners
    subscription = Subscription(
        mode="MERGE",
        items=list_,
        fields=fields_,
        )
    subscription.addlistener(on_charts_update)
    # subscription.addlistener(on_charts_update)
    # subscription = ig_stream_service.create_price_subscription(list_, fields_, on_prices_update)
    ig_stream_service.ls_client.subscribe(subscription)

    
    
    # you can also subscribe to account updates (balance)
    # subscription_account = ig_stream_service.create_balance_subscription(on_account_update)
    # ig_stream_service.connect(subscription_account)

    # finally, keep the client running
    while True:
        time.sleep(1)

相关问题