我有這樣的例子代碼:的Python - 線程 - 同時執行
# 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()
如何從類「守護」的運行(個體經營)函數退出並執行了firstFunction會()一樣寫在最後一行?我是一個Python的新手,我試圖學習
#編輯 我設法將守護程序類實現到treading類。但是我首先處於相同的情況,腳本停留在守護進程類中,不執行其他行。
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()。 –
'run()'方法中的'while True'條件將永遠保持您的程序在循環中。你打算有其他的條件嗎? – Sanjay
實際上,我必須在類「守護進程」的def run(self)中編寫下一個指令,但此刻我沒有寫任何內容,因爲我問了我可以做什麼來傳遞給其他類的命令,如最後一行「firstfunction()」。我能怎麼做? – AllExJ