gpt4 book ai didi

python - Python-启动线程会使用sys.exit()杀死所有线程

转载 作者:行者123 更新时间:2023-12-03 13:16:48 29 4
gpt4 key购买 nike

我有线程问题。基本上,当我执行threading.Thread(target=self.d3JpdGVDU1Y(payload)).start()时,这意味着它将创建一个带有目标和该函数参数的新线程。

我的问题是,当我在该线程上执行sys.exit()时,即使应该继续运行main():函数,它也会停止整个脚本,因为我们只是为不同的函数创建一个新线程。

def main():
# Discord send
if self.discordWebhook:
threading.Thread(
target=sendtoWebhook.c2VuZFRvRGlzY29yZA(self, self.discordWebhook, payload)).start()

# Send to CSV write
threading.Thread(target=self.d3JpdGVDU1Y(payload)).start()

--------------------------------

class sendtoWebhook:

def c2VuZFRvRGlzY29yZA(self, webhook, payload):
response = requests.post('{}/slack'.format(webhook), json=discord)

if response.status_code == 200:
print("Sent to discord!")
sys.exit()

elif response.status_code == 429:
print("Failed printing")
sys.exit()


它应该做的是杀死sendtoWebhook函数的线程,而不是全部使用sys.exit()进行操作,否则我做错了吗?

最佳答案

您的代码永远不会启动线程。

main()中,您可以执行以下操作:

Thread(target=func_that_calls_sys_exit(foo, bar), ...)

当您打算这样做时:
Thread(target=func_that_calls_sys_exit, args=(foo, bar), ...)

前者在创建任何线程之前调用该方法,旨在将其返回值传递给 Thread构造函数的 target参数。该调用当然会引发一个 SystemExit,因此可以在创建任何线程之前终止您的进程。

关于python - Python-启动线程会使用sys.exit()杀死所有线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60799643/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com