简体   繁体   English

从 try/except 块中包含的线程终止程序

[英]Terminating program from a thread contained in try/except block

can someone help me with terminating this program from if statement.有人可以帮我从 if 语句中终止这个程序。 I can't get it done.我无法完成它。 I've tried to this with sys.quit, but it seems to be not suitable for try/except block and I can't break out of the loop within thread.我已经尝试使用 sys.quit 进行此操作,但它似乎不适合 try/except 块,并且我无法跳出线程内的循环。 I could do this in the run() method, but it's a little bit useless to build a thread and then try to do something outside of it.我可以在 run() 方法中做到这一点,但是构建一个线程然后尝试在它之外做一些事情有点没用。 It feels like there is something wrong about it.感觉好像有什么不对劲。 Here's code:这是代码:

class TradingBot:

def init(self) -> None:
    self.api = tradeapi.REST(key_id=API_KEY, secret_key=SECRET_KEY, base_url=BASE_URL, api_version='v2')

def simple_thread(self):

    try:
        account = self.api.get_account()
        clock = self.api.get_clock()

        balance_change = float(account.equity) - float(account.last_equity)

        condition_1 = balance_change > 0
        condition_2 = balance_change < 0

        if condition_1:

            pass
            #Figure out something to quit if condition 1 is met

        elif condition_2:

            pass
            #Figure out something to quit if condition 2 is met


    except:

        print('Some error has occured')

def run(self):

    while True:

        execute = threading.Thread(target=self.simple_thread())
        execute.start()
        time.sleep(1)

sys.exit attempts to exit by throwing a SystemExit exception, which is caught by the bare except clause. sys.exit尝试通过抛出SystemExit异常退出,该异常被裸except子句捕获。 I'm not sure why you need the try / except at all, but you should probably at least change it to catch only Exception , which, unlikeBaseException , only includes normal exceptions, not special ones such as SystemExit .我不确定你为什么需要try / except ,但你可能至少应该将它更改为仅捕获Exception ,与BaseException不同,它只包括正常异常,而不包括诸如SystemExit之类的特殊异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自 try/except 块中使用的输入 - Input from the use in a try/except block 为什么在 if 语句中添加 try except 块并且程序说变量未定义 - Why adding try except block in if statement and the program says variable not defined 列表,元组和统计程序尝试-除块错误 - Lists, Tuples, and Statistics Program Try-Except Block Error 尝试与除外和线程错误 - Error with try and except and thread IndexError:超出测试用例 1 和 2 的界限; 使用 try-except 块来防止程序在 value3 > value1 时崩溃 - IndexError: Out of bounds for test case 1 and 2; using a try-except block to prevent the program from crashing when value3 > value1 除了在尝试了try块和程序的其余部分之后总是抛出块之外? - Except block always being thrown even after going through the try block and rest of the program? n 秒后终止 python 中的尝试块 - Terminating try block in python after n seconds Class 继承自 ZeroDivisionError 并在 try-except 块中使用。 except 块不被执行 - Class Inherited from ZeroDivisionError and used it in try-except block. except block is not executed 尝试从内部 Try/except 块中断外部 while 循环 - Trying to break outer while loop from inner Try/except block 代码设计:从try块重新发送消息给Rabbit - Code Design: resend a message to Rabbit from try except block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM