【发布时间】:2017-07-19 23:32:52
【问题描述】:
我有这个示例代码:
# some imports that I'm not including in the question
class daemon:
def start(self):
# do something, I'm not including what this script does to not write useless code to the question
self.run()
def run(self):
"""You should override this method when you subclass Daemon.
It will be called after the process has been daemonized by
start() or restart().
"""
class MyDaemon(daemon):
def run(self):
while True:
time.sleep(1)
if __name__ == "__main__":
daemonz = MyDaemon('/tmp/daemon-example.pid')
daemonz.start()
def firstfunction():
# do something
secondfunction()
def secondfunction():
# do something
thirdfunction()
def thirdfunction():
# do something
# here are some variables set that I am not writing
firstfunction()
如何退出“daemon”类的 run(self) 函数并继续执行 firstfunction() 就像最后一行中写的那样?我是 Python 新手,我正在努力学习
#编辑 我设法将守护程序类实现到踩踏类中。但我的情况与第一个相同,脚本停留在守护程序类中,不执行其他行。
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def daemonize(self):
# istructions to daemonize my script process
def run(self):
self.daemonize()
def my_function():
print("MyFunction executed") # never executed
thread = MyThread()
thread.start()
my_function() # the process is successfully damonized but
# this function is never executed
【问题讨论】:
-
您的代码会卡在 MyDaemon 的 while 循环中,不是吗?这就是为什么你的代码永远不会到达 firstfunction()。
-
while True方法中的while True条件将使您的程序永远处于循环中。您是否打算改用其他条件? -
实际上我必须在“daemon”类的 def run(self) 中编写下一条指令,但目前我什么都没写,因为我问我能做些什么来传递给另一个类外的命令,如最后一行“firstfunction()”。我该怎么办?
-
只是出于好奇,您是否会每天都问同一个问题(并删除前一个问题),直到有人为您编写项目或至少是线程方面的课程级教程?跨度>
标签: python python-3.x class object