是否可以通过线程等待 Try Else 块执行?
在下面附加的代码中,有什么方法可以让我等待 else 块打印语句仅在线程完成后当前执行 由于函数 gg 是线程化的,我正在获取输出
HelloNothing went wrong
Hello
预期输出
Hello
Hello
Nothing went wrong
当前代码
import threading
import time
def gg():
print("Hello")
time.sleep(5)
print("Hello")
try:
threading.Thread(target=gg).start()
except:
print("Something went wrong")
else:
print("Nothing went wrong")
In the below code attached is there any way I can wait for else block print statement to get executed only after threading is done currently
as the function gg is threaded I am getting output
HelloNothing went wrong
Hello
Expected output
Hello
Hello
Nothing went wrong
Current code
import threading
import time
def gg():
print("Hello")
time.sleep(5)
print("Hello")
try:
threading.Thread(target=gg).start()
except:
print("Something went wrong")
else:
print("Nothing went wrong")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用加入方法
You can use Join method