2017-07-24 170 views
0

我寫了litte加密切換器,它檢查利潤和開關礦工。但它有一段時間,然後循環停止沒有任何錯誤(它可能會工作15分鐘或20小時,但它會停止,測試10天)。無限while循環有時打破

代碼:

import os 
import subprocess 
import time 
import copy 
import requests 
import configparser 
from datetime import datetime 


def config_read(): 
    config = configparser.ConfigParser() 
    config.read('config.ini') 
    return config.sections 


def start_miner(info): 
    if info['algorithm'] == 'Equihash': 
     subprocess.Popen('F:\Claymore\start — music — Peon.bat', cwd='F:\Claymore', 
         creationflags=subprocess.CREATE_NEW_CONSOLE) 
    elif info['algorithm'] == 'Ethash': 
     subprocess.Popen('F:\Claymore\start — music — Peon.bat', cwd='F:\Claymore', 
         creationflags=subprocess.CREATE_NEW_CONSOLE) 
    return info 


def stop_miner(): 
    os.system("taskkill /f /t /im miner.exe") 
    os.system("taskkill /f /t /im EthDcrMiner64.exe") 


def request_coins(): 
    coins = None 
    while coins is None: 
     try: 
      coins = ((requests.get(
       url='https://whattomine.com/coins.json?utf8=✓&eth=true&factor%5Beth_hr%5D=79.0&factor%5Beth_p%5D=0.0&factor%5Bgro_hr%5D=0.0&factor%5Bgro_p%5D=0.0&factor%5Bx11g_hr%5D=20.0&factor%5Bx11g_p%5D=0.0&factor%5Bcn_hr%5D=0.0&factor%5Bcn_p%5D=0.0&eq=true&factor%5Beq_hr%5D=1000.0&factor%5Beq_p%5D=0.0&factor%5Blrev2_hr%5D=80000.0&factor%5Blrev2_p%5D=0.0&factor%5Bns_hr%5D=0.0&factor%5Bns_p%5D=0.0&factor%5Blbry_hr%5D=0.0&factor%5Blbry_p%5D=0.0&factor%5Bbk2b_hr%5D=0.0&factor%5Bbk2b_p%5D=0.0&factor%5Bbk14_hr%5D=0.0&factor%5Bbk14_p%5D=0.0&factor%5Bpas_hr%5D=0.0&factor%5Bpas_p%5D=0.0&bkv=true&factor%5Bbkv_hr%5D=0.0&factor%5Bbkv_p%5D=0.0&factor%5Bcost%5D=0.06&sort=Profitability24&volume=0&revenue=24h&factor%5Bexchanges%5D%5B%5D=&factor%5Bexchanges%5D%5B%5D=bittrex&factor%5Bexchanges%5D%5B%5D=bleutrade&factor%5Bexchanges%5D%5B%5D=btc_e&factor%5Bexchanges%5D%5B%5D=bter&factor%5Bexchanges%5D%5B%5D=c_cex&factor%5Bexchanges%5D%5B%5D=cryptopia&factor%5Bexchanges%5D%5B%5D=poloniex&factor%5Bexchanges%5D%5B%5D=yobit&dataset=Main&commit=Calculate&adapt_q_280x=0&adapt_q_380=0&adapt_q_fury=0&adapt_q_470=0&adapt_q_480=0&adapt_q_750Ti=0&adapt_q_10606=3&adapt_q_1070=0&adapt_q_1080=0&adapt_q_1080Ti=0%27')).json())[ 
      'coins'] 
     except: 
      print("Site didn't respond. Reconnecting in 10 sec") 
      time.sleep(10) 
    return coins 


def miner_chose(config, info): 
    user_coins = {} 
    coins = request_coins() 
    for key, value in config['Currency'].items(): 
     if value == 'True': 
      tag = key.upper() 
      for key_coin, value_coin in coins.items(): 
       if value_coin['tag'] == info['temp_currency']: 
        info['temp_profit'] = value_coin['btc_revenue24'] 
       if value_coin['tag'] == info['currency']: 
        info['profit'] = value_coin['btc_revenue24'] 
       if value_coin['tag'] == tag: 
        user_coins[key_coin] = value_coin 
    for key, value in user_coins.items(): 
     if float(value['btc_revenue24']) >= float(info['profit']) * (float(config['CheckOptions']['profitprocent']) +100)/100: 
      if not info['currency'] == value['tag']: 
       if float(value['btc_revenue24']) > float(info['temp_profit']): 
        if not info['temp_currency'] == value['tag']: 
         info['check_times'] = 0 
        info['temp_profit'] = value['btc_revenue24'] 
        info['temp_currency'] = value['tag'] 
       info['check_times'] += 1 
       if int(info['check_times']) >= int(config['CheckOptions']['times']): 
        info['profit'] = value['btc_revenue24'] 
        info['currency'] = value['tag'] 
        info['algorithm'] = value['algorithm'] 
        info['check_times'] = 0 
    return info 

的代碼的一部分,雖然循環使用和停止:

def main(): 
    info = {'profit': 0, 'check_times': 200, 'currency': None, 'temp_profit': 0, 'temp_currency': None} 
    config = config_read() 

    while True: 
     if info['profit'] == 0: 
      stop_miner() 
      info = start_miner(miner_chose(config,info)) 
      print(str(datetime.now()) + " - Starting miner first time. Currency: " + info['currency'] + '. Profit: ' + 
        info['profit'] + ' BTC/Day') 
      time.sleep(int(config['CheckOptions']['period']) * 60) 
     else: 
      old_info = copy.deepcopy(info) 
      info = miner_chose(config, info) 
      print(
       str(datetime.now()) + ' - Checking profit. Current currency: ' + info['currency'] + '. Profit: ' + info[ 
        'profit'] + ' BTC/Day') 
      if info['currency'] != old_info['currency']: 
       print('Changing miner. Currency ' + info['currency'] + '. Profit: ' + info['profit'] + ' BTC/Day') 
      elif info['currency'] == old_info['currency']: 
       print('Curency SAME') 
       # stop_miner() 
       # start_miner(info) 
      time.sleep(int(config['CheckOptions']['period']) * 60) 

我想要睡覺time.sleep(int(config['CheckOptions']['period']) * 60)腳本後,必須重新開始,再次使所有的檢查,但有時候劇本睡眠對於我已經放入配置的時間並且不想再次檢查。它可能在10或20次檢查後停止,沒有任何錯誤。

+0

什麼循環?有多個。 –

+3

這是太多的代碼。只發布相關部分並明確描述問題 –

+0

請留下完整的代碼,但添加停止的while循環。 – Hellbea

回答

0

需要添加到coins = ((requests.get(url='https://whattomine.com/coins.json', timeout=3)).json())['coins']導致服務器堆棧並且不發送響應。