【问题标题】:Windows batch file Infinite Loop hang while running Python fileWindows批处理文件无限循环在运行Python文件时挂起
【发布时间】:2019-07-29 20:40:46
【问题描述】:

我有一个非常简单的 Windows 批处理文件,它应该运行 Python,等待 5 秒,回显搜索,循环。 Python 文件正在检查我的电子邮件并创建文件或删除它们,然后当条件适合我进行交易时通过电子邮件发送给我。 它工作得很好,但一夜之间我醒来看到它停滞不前。如果我按下回车,它会再次启动并运行一段时间。

批号是

:loop
timeout 5

"C:\Anaconda3\python.exe" "TValert.py"

echo "Scan Complete"

goto loop

而python代码是

import imaplib,email,time
import smtplib
import os.path
from os import path
#mail imap
user = 'Trader@gmail.com'
pwd = 'password'
imap_url = 'imap.gmail.com'
con = imaplib.IMAP4_SSL (imap_url)
con.login(user,pwd)
con.select('INBOX')


def deleteEmail(user, pwd, IMAP):

    typ, data = con.search(None, 'ALL')
    for num in data[0].split():
        con.store(num, '+FLAGS', r'(\Deleted)')
        con.expunge()



print("Scanning Email....")
time.sleep(1.5)
result, no = con.search(None,'(FROM "god" SUBJECT "Come Home")')
result, long = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Long")')
result, short = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Short")')
result, close_long = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Close Long")')
result, close_short = con.search(None,'(FROM "tradingview" SUBJECT "Rosie Close Short")')
result, TwoRiskOff = con.search(None,'(FROM "tradingview" SUBJECT "$2 Risk Off")')    
result, NineRiskOff = con.search(None,'(FROM "tradingview" SUBJECT "$9 Risk Off")')    
result, TwoWhite = con.search(None,'(FROM "tradingview" SUBJECT "$2 White")')    
result, NineWhite = con.search(None,'(FROM "tradingview" SUBJECT "$9 White")')      
result, TwoBlack = con.search(None,'(FROM "tradingview" SUBJECT "$2 Black")')    
result, NineBlack = con.search(None,'(FROM "tradingview" SUBJECT "$9 Black")')      

if long != no:
    if path.exists("Long.txt"):
        mail=smtplib.SMTP('smtp.gmail.com',587)
        mail.ehlo()
        mail.starttls()
        mail.login(user,pwd)
        message = 'Subject: {}\n\n{}'.format("ERROR DUPLICATE LONG SIGNAL","ERROR DUPLICATE LONG SIGNAL" )    
        mail.sendmail(user,"2062348485@mms.att.net",message)
        mail.close

    else:
        if path.exists("Short.txt"):
            os.remove("Short.txt")
            mail=smtplib.SMTP('smtp.gmail.com',587)
            mail.ehlo()
            mail.starttls()
            mail.login(user,pwd)
            message = 'Subject: {}\n\n{}'.format("Close Long Position","Close Long Position" )    
            mail.sendmail(user,"2062348485@mms.att.net",message)
            mail.close

        else:
            if path.exists("TwoWhite.txt"):
                if path.exists("NineWhite.txt"):
                    open("Long.txt","w+")
                    mail=smtplib.SMTP('smtp.gmail.com',587)
                    mail.ehlo()
                    mail.starttls()
                    mail.login(user,pwd)
                    message = 'Subject: {}\n\n{}'.format("BUY NOW!", "BUY NOW!")    
                    mail.sendmail(user,"2062348485@mms.att.net",message)
                    mail.close
                else:
                    print("No Correlation")
            else:
                print("No Correlation")





if short != no:
    if path.exists("Short.txt"):
        mail=smtplib.SMTP('smtp.gmail.com',587)
        mail.ehlo()
        mail.starttls()
        mail.login(user,pwd)
        message = 'Subject: {}\n\n{}'.format("ERROR DUPLICATE SHORT SIGNAL","ERROR DUPLICATE SHORT SIGNAL" )    
        mail.sendmail(user,"2062348485@mms.att.net",message)
        mail.close

    else:
        if path.exists("Long.txt"):
            os.remove("Long.txt")
            mail=smtplib.SMTP('smtp.gmail.com',587)
            mail.ehlo()
            mail.starttls()
            mail.login(user,pwd)
            message = 'Subject: {}\n\n{}'.format("Close Long Position","Close Long Position" )    
            mail.sendmail(user,"2062348485@mms.att.net",message)
            mail.close

        else:
            if path.exists("TwoBlack.txt"):
                if path.exists("NineBlack.txt"):
                    open("Short.txt","w+")
                    mail=smtplib.SMTP('smtp.gmail.com',587)
                    mail.ehlo()
                    mail.starttls()
                    mail.login(user,pwd)
                    message = 'Subject: {}\n\n{}'.format("SELL NOW!", "SELL NOW!")    
                    mail.sendmail(user,"2062348485@mms.att.net",message)
                    mail.close
                else:
                    print("No Correlation")
            else:
                print("No Correlation")





if TwoRiskOff !=no:
    try:
        if path.exists("TwoBlack.txt"):
            os.remove("TwoBlack.txt")
        if path.exists("TwoWhite.txt"):
            os.remove("TwoWhite.txt")
    except:
        print("Error While Running Two Risk Off")

if NineRiskOff !=no:
    try:
        if path.exists("NineBlack.txt"):
            os.remove("NineBlack.txt")
        if path.exists("NineWhite.txt"):
            os.remove("NineWhite.txt")
    except:
        print("Error While Running Two White")

if TwoWhite !=no:
    try:
        open("TwoWhite.txt","w+")
        if path.exists("TwoBlack.txt"):
            os.remove("TwoBlack.txt")
    except:
        print("Error While running Two White")


if TwoBlack !=no:
    try:
        open("TwoBlack.txt","w+")
        if path.exists("TwoWhite.txt"):
            os.remove("TwoWhite.txt")    
    except:
        print("Error While running Two Black")

if NineWhite !=no:
    try:
        open("NineWhite.txt","w+")
        if path.exists("NineBlack.txt"):
            os.remove("NineBlack.txt")
    except:
        print("Error While running Nine White")

if NineBlack !=no:
    try:
        open("NineBlack.txt","w+")  
        if path.exists("NineWhite.txt"):
            os.remove("NineWhite.txt")
    except:
        print("Error While running Nine Black") 

deleteEmail(user,pwd,con)


exit()


【问题讨论】:

  • IB 的代码就是这样断开套接字的。他们对代码进行了一些更改,但我不知道它将在哪个版本中发布。您可以忽略它。

标签: python python-3.x windows batch-file cmd


【解决方案1】:

我今天和我的一个朋友交谈,他指示我通过 Powershell 运行 python 文件,到目前为止它运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2018-10-01
    • 2011-07-26
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多