【问题标题】:Python - Threading - execute simultaneouslyPython - 线程 - 同时执行
【发布时间】: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


【解决方案1】:

您可以使用break关键字退出循环,然后继续下一行。 return 可用于退出函数。

class daemon:
    def start(self):
        self.run()

    def run(self):
        while True:
            break
        return
        print()  # This never executes

如果您希望 MyDaemon 与您的其余代码一起运行,您必须将其设为进程或线程。然后代码自动继续到下一行,同时运行 MyDaemon 类(线程/进程)。

import threading  


class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("Thread started")
        while True:
            pass


def my_function():
    print("MyFunction executed")

thread = MyThread()
thread.start()        # executes run(self)
my_function()

此代码产生以下结果:

Thread started
MyFunction executed

要使thread 成为守护进程,您可以使用thread.setDaemon(True)。该函数必须在线程启动之前调用:

thread = MyThread()
thread.setDaemon(True)
thread.start()
my_function()

【讨论】:

  • @AllExJ 不太确定。很久没有用 Python 编程了。如果我是对的,您将需要多线程或多处理。如果您的程序退出时进程将退出,那么您将不需要守护进程。启动进程时,代码在进程运行时继续到下一行。我将查找多线程,并在此处创建一个示例。 :)
  • 非常感谢!!所以在函数“my_function”中我必须把代码转换为守护进程对吗?
  • 如果我想对代码的其他部分使用另一个线程类?我可以这样做我必须使用“MyTread”类吗?
  • @AllExJ 感谢您的帮助。如果这是您正在寻找的答案,您可以标记它吗? :) 希望一切都解决了。
  • 终端告诉我:“RuntimeError: cannot set daemon status of active thread”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-24
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
相关资源
最近更新 更多